Statement: this instance is not original and is reprinted from the original! Attachment: 1. Attach a basic console operation file instance: http://blog.sina.com.cn/s/blog_4dfe2d620100d0f8.html,c! 2, C # file File Operations Daquan: http://www.cnblogs.com/jiasongmao/archive/2011/04/27/2031156.html Using system; Using system. Collections. Generic; Using system. componentmodel; Using system. Data; Using system. drawing; Using system. LINQ; Using system. text; Using system. Windows. forms; Using system. IO; Namespace windowsformsfilesystem { Public partial class form1: Form { Public form1 () { Initializecomponent (); } /// <Summary> /// When you click the Browse button, open folderbrowserdialog1 and assign the path selected by the user to textbox1 /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button#click (Object sender, eventargs E) { Try { If (folderbrowserdialog1.showdialog () = system. Windows. Forms. dialogresult. OK) { Textbox1.text = folderbrowserdialog1.selectedpath; } } Catch (exception ee) { MessageBox. Show (EE. Message ); } } /// <Summary> /// Select a file /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button2_click (Object sender, eventargs E) { Try { If (openfiledialog1.showdialog () = system. Windows. Forms. dialogresult. OK) { Textbox2.text = openfiledialog1.filename; // complete file path } } Catch (exception ee) { MessageBox. Show (EE. Message ); } } /// <Summary> /// File copy operation /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button3_click (Object sender, eventargs E) { If (! File. exists (textbox2.text )) { MessageBox. Show ("select the correct file path "); Return; } If (folderbrowserdialog2.showdialog () = system. Windows. Forms. dialogresult. OK) { Fileinfo file1 = new fileinfo (textbox2.text ); // MessageBox. Show (file1.name); // file name String destfilepath = folderbrowserdialog2.selectedpath + file1.name; // target Path = target directory + original file name File. Copy (textbox2.text, destfilepath ); MessageBox. Show ("file copied successfully ~ "); } } /// <Summary> /// File Movement /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button4_click (Object sender, eventargs E) { If (! File. exists (textbox2.text )) { MessageBox. Show ("select the correct file path "); Return; } If (folderbrowserdialog2.showdialog () = system. Windows. Forms. dialogresult. OK) { Try { Fileinfo file1 = new fileinfo (textbox2.text ); // MessageBox. Show (file1.name); // file name // String destfilepath = folderbrowserdialog2.selectedpath + file1.name; // target Path = target directory + original file name String destfilepath = folderbrowserdialog2.selectedpath + path. getfilename (textbox2.text); // obtain the file directory based on the complete file path. File. Move (textbox2.text, destfilepath ); MessageBox. Show ("file moved successfully ~ "); } Catch (exception ee) { MessageBox. Show ("file moving failed ~ "+ Ee. Message ); }
} } /// <Summary> /// Delete an object /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button5_click (Object sender, eventargs E) { If (! File. exists (textbox2.text )) { MessageBox. Show ("select the correct file path "); Return; } // Use R to indicate the return value of the dialog box Dialogresult r = MessageBox. Show ("are you sure you want to delete the file? "," File Deletion prompt ", messageboxbuttons. yesnocancel, messageboxicon. Warning ); If (r = system. Windows. Forms. dialogresult. Yes) { File. Delete (textbox2.text ); MessageBox. Show ("File deleted "); } } Private void button6_click (Object sender, eventargs E) { If (! Directory. exists (textbox1.text )) { MessageBox. Show ("select a directory ~ "); Return; } String [] files = directory. getfiles (textbox1.text ); Foreach (string file in files) { Listbox1.items. Add (File ); } String [] directories = directory. getdirectories (textbox1.text ); Foreach (string directory in directories) { // } } /// <Summary> /// Open the file /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button11_click (Object sender, eventargs E) { Openfiledialog DLG = new openfiledialog (); DLG. Filter = "all files | *.*"; If (DLG. showdialog () = system. Windows. Forms. dialogresult. OK) { Textbox2.text = DLG. filename; Fileinfo Fi = new fileinfo (DLG. filename ); MessageBox. Show (Fi. Name + fi. Length + fi. fullname ); } } /// <Summary> /// Write characters to the stream to read characters from the byte stream /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void button10_click (Object sender, eventargs E) { // Create streamwriter to prepare for writing Streamwriter RW = file. createtext ("E: \ hitest.txt "); // Write content using writeline RW. writeline ("writing a text stream to the text being created or opened ..."); RW. writeline ("harhatland "); // Write the buffer content to the file RW. Flush (); // Close the RW object RW. Close (); // Open a text file Streamreader sr = file. opentext ("E: \ hitest.txt "); Stringbuilder output = new stringbuilder (); String RL; While (RL = Sr. Readline ())! = NULL) { Output. append (RL + "\ r \ n "); } MessageBox. Show (output. tostring ()); Sr. Close (); } Private void button8_click (Object sender, eventargs E) { Filestream FS = new filestream ("E: \ fstest.txt", filemode. Create ); // Create streamwriter to prepare for writing Streamwriter RW = new streamwriter (FS, encoding. Default ); // Write content using writeline RW. writeline ("Microsoft is equal to micro plus soft .."); RW. writeline ("hahahahahahahahahahahahahahaha and "); // Write the buffer content to the file RW. Flush (); // Close the RW object RW. Close (); FS. Close (); FS = new filestream ("E: \ fstest.txt", filemode. Open, fileaccess. readwrite ); Streamreader sr = new streamreader (FS, encoding. Default ); Stringbuilder output = new stringbuilder (); String RL; While (RL = Sr. Readline ())! = NULL) { Output. append (RL + "\ r \ n "); } MessageBox. Show (output. tostring ()); Sr. Close (); FS. Close (); } } } |