1 //C # append file2StreamWriter SW = File.appendtext (Server.MapPath (".")+"\\myText.txt"); 3Sw. WriteLine ("chasing ideals"); 4Sw. WriteLine ("kzlll"); 5Sw. WriteLine (". NET Notes"); 6 SW. Flush (); 7 SW. Close (); 8 9 //C # Copy filesTen stringOrignfile,newfile; OneOrignfile = Server.MapPath (".")+"\\myText.txt"; ANewFile = Server.MapPath (".")+"\\myTextCopy.txt"; -File.Copy (Orignfile,newfile,true); - the //C # Delete files - stringDelfile = Server.MapPath (".")+"\\myTextCopy.txt"; - File.delete (delfile); - + //C # moving files - stringOrignfile,newfile; +Orignfile = Server.MapPath (".")+"\\myText.txt"; ANewFile = Server.MapPath (".")+"\\myTextCopy.txt"; at File.move (orignfile,newfile); - - //C # Create a directory - //Create directory C:\sixAge -DirectoryInfo D=directory.createdirectory ("C:\\sixage"); - //D1 point to C:\sixAge\sixAge1 inDirectoryInfo D1=d.createsubdirectory ("SixAge1"); - //D2 point to C:\sixAge\sixAge1\sixAge1_1 toDirectoryInfo d2=d1. Createsubdirectory ("sixage1_1"); + //set the current directory to C:\sixAge -Directory.setcurrentdirectory ("C:\\sixage"); the //Create directory C:\sixAge\sixAge2 *Directory.CreateDirectory ("SixAge2"); $ //Create directory C:\sixAge\sixAge2\sixAge2_1Panax NotoginsengDirectory.CreateDirectory ("Sixage2\\sixage2_1"); - the //recursively delete folders and files +<%@ Page language=c#%> A<%@ Importnamespace="System.IO"%> the<script runat=server> + Public voidDeleteFolder (stringdir) - { $ if(Directory.Exists (dir))//If there is a deletion of this folder $ { - foreach(stringDinchdirectory.getfilesystementries (dir)) - { the if(file.exists (d)) -File.delete (d);//Delete files directly from themWuyi Else theDeleteFolder (d);//recursively Delete Subfolders - } WuDirectory.delete (dir);//Delete an empty folder -Response.Write (dir+"Folder deletion succeeded"); About } $ Else -Response.Write (dir+"The folder does not exist");//Prompt if the folder does not exist - } - A protected voidPage_Load (Object sender, EventArgs e) + { the stringDir="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.
// ======================================================
1 Public Static voidCopydir (stringSrcpath,stringAimpath)2 {3 Try4 {5 //check whether the destination directory ends with a directory split character if it is not added6 if(aimpath[aimpath.length-1] !=Path.directoryseparatorchar)7Aimpath + =Path.directoryseparatorchar;8 //determine if the target directory exists and if it does not exist, create a new9 if(!directory.exists (aimpath))Ten directory.createdirectory (aimpath); One //get a list of the files in the source directory, which is an array containing the files and directory paths A //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); the //traverse all files and directories - foreach(stringFileinchfileList) - { - //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 A Else atFile.Copy (file,aimpath+path.getfilename (file),true); - } - } - Catch(Exception e) - { - MessageBox.Show (e.tostring ()); in } - } to + - Public Static voidDeletedir (stringAimpath) the { * Try $ {Panax Notoginseng //check whether the destination directory ends with a directory split character if it is not added - if(aimpath[aimpath.length-1] !=Path.directoryseparatorchar) theAimpath + =Path.directoryseparatorchar; + //get a list of the files in the source directory, which is an array containing the files and directory paths A //If you point to the file below the delete target file and do not include the directory, use the following method the //string[] fileList = Directory.GetFiles (Aimpath); + string[] FileList =directory.getfilesystementries (aimpath); - //traverse all files and directories $ foreach(stringFileinchfileList) $ { - //as directory processing, if the directory is present, recursively delete the file under the directory. - if(directory.exists (file)) the { -Deletedir (aimpath+path.getfilename (file));Wuyi } the //Otherwise, delete the file directly - Else Wu { -File.delete (aimpath+path.getfilename (file)); About } $ } - //Delete a folder -System.IO. Directory. Delete (Aimpath,true); - } A Catch(Exception e) + { the MessageBox.Show (e.tostring ()); - } $}
C # for file operations