StreamWriter sw = File. AppendText (Server. MapPath (".") + "\ myText.txt ");
Sw. WriteLine ("Chasing ideals ");
Sw. WriteLine ("kzlll ");
Sw. WriteLine (". NET notes ");
Sw. Flush ();
Sw. Close ();
C # copy an object
String OrignFile, NewFile;
OrignFile = Server. MapPath (".") + "\ myText.txt ";
NewFile = Server. MapPath (".") + "\ myTextCopy.txt ";
File. Copy (OrignFile, NewFile, true );
C # delete an object
String delFile = Server. MapPath (".") + "\ myTextCopy.txt ";
File. Delete (delFile );
C # Move files
String OrignFile, NewFile;
OrignFile = Server. MapPath (".") + "\ myText.txt ";
NewFile = Server. MapPath (".") + "\ myTextCopy.txt ";
File. Move (OrignFile, NewFile );
C # create a directory
// Create the directory c: sixAge
DirectoryInfo d = Directory. CreateDirectory ("c: \ sixAge ");
// D1 points to c: sixAgesixAge1
DirectoryInfo d1 = d. CreateSubdirectory ("sixAge1 ");
// D2 points to c: sixAgesixAge1sixAge1_1
DirectoryInfo d2 = d1.CreateSubdirectory ("sixAge1_1 ");
// Set the current directory to c: sixAge
Directory. SetCurrentDirectory ("c: \ sixAge ");
// Create the directory c: sixAgesixAge2
Directory. CreateDirectory ("sixAge2 ");
// Create the directory c: sixAgesixAge2sixAge2_1
Directory. CreateDirectory ("sixAge2 \ sixAge2_1 ");
Recursively delete folders and files
<% @ Page Language = C # %>
<% @ Import namespace = "System. IO" %>
<Script runat = server>
Public void DeleteFolder (string dir)
{
If (Directory. Exists (dir) // if this folder Exists, delete it
{
Foreach (string d in Directory. GetFileSystemEntries (dir ))
{
If (File. Exists (d ))
File. Delete (d); // Delete the File directly.
Else
DeleteFolder (d); // recursively Delete subfolders
}
Directory. Delete (dir); // Delete an empty folder
Response. Write (dir + "folder deleted successfully ");
}
Else
Response. Write (dir + "this folder does not exist"); // If the folder does not exist, a prompt is displayed.
}
Protected void Page_Load (Object sender, EventArgs e)
{
String Dir = "D: \ gbook \ 11 ";
DeleteFolder (Dir); // call the function to delete a folder
}
// ================================================ ======================
// Implement a static method to copy all the content in the specified folder to the target folder
// If the target folder is read-only, an error is returned.
// April 18April2005 In STU
// ================================================ ======================
Public static void CopyDir (string srcPath, string aimPath)
{
Try
{
// Check whether the target directory ends with a directory delimiter. If not, add it.
If (aimPath [aimPath. Length-1]! = Path. DirectorySeparatorChar)
AimPath + = Path. DirectorySeparatorChar;
// Determine whether the target directory exists. If it does not exist, create it.
If (! Directory. Exists (aimPath) Directory. CreateDirectory (aimPath );
// Obtain the file list of the source Directory, which is an array containing the file and directory path
// Use the following method if you direct to the file under the copy target file without including the Directory
// String [] fileList = Directory. GetFiles (srcPath );
String [] fileList = Directory. GetFileSystemEntries (srcPath );
// Traverse all files and directories
Foreach (string file in fileList)
{
// Process the file as a directory first. If this directory exists, recursively Copy the file under this directory.
If (Directory. Exists (file ))
CopyDir (file, aimPath + Path. GetFileName (file ));
// Otherwise, Copy the file directly.
Else
File. Copy (file, aimPath + Path. GetFileName (file), true );
}
}
Catch (Exception e)
{
MessageBox. Show (e. ToString ());
}
}
// ================================================ ======================
// Implement a static method to Detele all content in the specified folder
// Perform the test with caution. The operation cannot be restored after deletion.
// April 18April2005 In STU
// ================================================ ======================
Public static void DeleteDir (string aimPath)
{
Try
{
// Check whether the target directory ends with a directory delimiter. If not, add it.
If (aimPath [aimPath. Length-1]! = Path. DirectorySeparatorChar)
AimPath + = Path. DirectorySeparatorChar;
// Obtain the file list of the source Directory, which is an array containing the file and directory path
// Use the following method if you direct to the file under the Delete target file without including the Directory
// String [] fileList = Directory. GetFiles (aimPath );
String [] fileList = Directory. GetFileSystemEntries (aimPath );
// Traverse all files and directories
Foreach (string file in fileList)
{
// Process the file as a directory first. If this directory exists, recursively Delete the files under this directory.
If (Directory. Exists (file ))
{
DeleteDir (aimPath + Path. GetFileName (file ));
}
// Otherwise, Delete the file directly.
Else
{
File. Delete (aimPath + Path. GetFileName (file ));
}
}
// Delete a folder
System. IO. Directory. Delete (aimPath, true );
}
Catch (Exception e)
{
MessageBox. Show (e. ToString ());
}
}
Namespace to be referenced:
Using System. IO;
/// <Summary>
/// Copy a folder (including subfolders) to the specified folder. Both the source folder and target folder must be in the absolute path. Format: CopyFolder (source folder,
Target folder );
/// </Summary>
/// <Param name = "strFromPath"> </param>
/// <Param name = "strToPath"> </param>
//--------------------------------------------------
// Prepared by: Go to dinner tomorrow QQ: 305725744
//---------------------------------------------------
Public static void CopyFolder (string strFromPath, string strToPath)
{
// If the source folder does not exist, create
If (! Directory. Exists (strFromPath ))
{
Directory. CreateDirectory (strFromPath );
}
// Obtain the name of the folder to be copied
String strFolderName = strFromPath. Substring (strFromPath. LastIndexOf ("\") + 1, strFromPath. Length
-StrFromPath. LastIndexOf ("\")-1 );
// If no source folder exists in the target folder, create the source folder in the target folder.
If (! Directory. Exists (strToPath + "\" + strFolderName ))
{
Directory. CreateDirectory (strToPath + "\" + strFolderName );
}
// Create an array to save the file name in the source folder
String [] strFiles = Directory. GetFiles (strFromPath );
// Copy objects cyclically
For (int I = 0; I <strFiles. Length; I ++)
{
// Get the copied file name. Only the file name is taken and the address is truncated.
String strFileName = strFiles [I]. Substring (strFiles [I]. LastIndexOf ("\") + 1, strFiles [I]. Length-
StrFiles [I]. LastIndexOf ("\")-1 );
// Start copying an object. true indicates overwriting an object of the same name.
File. Copy (strFiles [I], strToPath + "\" + strFolderName + "\" + strFileName, true );
}
// Create a DirectoryInfo instance
DirectoryInfo dirInfo = new DirectoryInfo (strFromPath );
// Obtain the names of all subfolders in the source folder
DirectoryInfo [] ZiPath = dirInfo. GetDirectories ();