Code
C # Append file
StreamWriter SW = File.appendtext (Server.MapPath (".") + "\\myText.txt");
Sw. WriteLine ("Chasing the ideal");
Sw. WriteLine ("kzlll");
Sw. WriteLine (". NET Notes ");
Sw. Flush ();
Sw. Close ();
C # Copy files
String Orignfile,newfile;
Orignfile = Server.MapPath (".") + "\\myText.txt";
NewFile = Server.MapPath (".") + "\\myTextCopy.txt";
File.Copy (orignfile,newfile,true);
C # Delete Files
String delfile = Server.MapPath (".") + "\\myTextCopy.txt";
File.delete (Delfile);
C # moving files
String Orignfile,newfile;
Orignfile = Server.MapPath (".") + "\\myText.txt";
NewFile = Server.MapPath (".") + "\\myTextCopy.txt";
File.move (Orignfile,newfile);
C # Create a directory
Create Directory C:\sixAge
DirectoryInfo d=directory.createdirectory ("C:\\sixage");
D1 point to C:\sixAge\sixAge1
DirectoryInfo d1=d.createsubdirectory ("SixAge1");
D2 point to C:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1. Createsubdirectory ("Sixage1_1");
Set the current directory to C:\sixAge
Directory.setcurrentdirectory ("C:\\sixage");
Create Directory C:\sixAge\sixAge2
Directory.CreateDirectory ("SixAge2");
Create Directory C:\sixAge\sixAge2\sixAge2_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 there is a deletion of this folder
{
foreach (String D in Directory.getfilesystementries (dir))
{
if (file.exists (d))
File.delete (d); Delete files directly from them
Else
DeleteFolder (d); Recursively Delete Subfolders
}
Directory.delete (dir); Delete an empty folder
Response.Write (dir+ "folder deletion succeeded");
}
Else
Response.Write (dir+ "This folder does not exist"); Prompt if the folder does not exist
}
protected void Page_Load (Object sender, EventArgs e)
{
String dir= "d:\\gbook\\11";
DeleteFolder (Dir); Calling a function to delete a folder
}
// ======================================================
Implement a static method copy all the contents below the specified folder to the target folder
If the destination folder is a read-only property, an error occurs.
April 18april2005 in STU
// ======================================================
Code
public static void Copydir (String srcpath,string aimpath)
{
Try
{
Check whether the destination directory ends with a directory split character if it is not added
if (aimpath[aimpath.length-1]! = Path.directoryseparatorchar)
Aimpath + = Path.directoryseparatorchar;
Determine if the target directory exists and if it does not exist, create a new
if (! Directory.Exists (Aimpath))
Directory.CreateDirectory (Aimpath);
Get a list of the files in the source directory, which is an array containing the files and directory paths
If you point to the file below the copy destination file and do not include the directory, use the following method
string[] fileList = Directory.GetFiles (Srcpath);
string[] fileList = directory.getfilesystementries (Srcpath);
Traverse All files and directories
foreach (string file in FileList)
{
As directory processing, if this directory is present, recursively copy the files under the directory
if (directory.exists (file))
Copydir (file,aimpath+path.getfilename (file));
Otherwise, the direct copy file
Else
File.Copy (file,aimpath+path.getfilename (file), true);
}
}
catch (Exception e)
{
MessageBox.Show (E.tostring ());
}
}
/ ======================================================
Implement a static method that will specify all the contents of the folder below Detele
Be careful when testing, and cannot be recovered after deletion.
April 18april2005 in STU
// ======================================================
Code
public static void Deletedir (String aimpath)
{
Try
{
Check whether the destination directory ends with a directory split character if it is not added
if (aimpath[aimpath.length-1]! = Path.directoryseparatorchar)
Aimpath + = Path.directoryseparatorchar;
Get a list of the files in the source directory, which is an array containing the files and directory paths
If you point to the file below the delete target file and do not include the directory, use the following method
string[] fileList = Directory.GetFiles (Aimpath);
string[] fileList = directory.getfilesystementries (Aimpath);
Traverse All files and directories
foreach (string file in FileList)
{
As directory processing, if the directory is present, recursively delete the file under the 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 ());
}
}
C # for file operations