C # full file operations

Source: Internet
Author: User

The most common method for Operating Files in C # Classic is as follows: C # append, copy, delete, move a file, create a directory, recursively delete a folder and a file, copy all the content under a specified folder to the target folder, and Detele and, reading text files, obtaining file lists, reading log files, writing log files, creating HTML files, and using the CreateDirectory Method

C # append an object
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>
 
/// </Summary>
/// <Param> </param & g

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.