The example here is C # writing Notepad
First: Solve file read and write problems
1. READ: File-Open Click Time is a new OpenFileDialog prompt box to select a file, read by using the FileStream file stream way
click Method
<span style= "White-space:pre" ></span>openfiledialog dlg = new OpenFileDialog (); if (dlg. ShowDialog () = = DialogResult.OK)//Determine whether to click on the OK button textbox1.text=tools. Filerw.readtext (dlg. Filename,encodinguse); FName = dlg. safefilename;//Global file name FilePath = dlg. Filename.substring (0, Dlg. Filename.indexof (FName));//Global file path
FileStream reading method
<summary>// //c# file stream read File///</summary>// <param name= "FilePath" ></param> // <param name= "readencoding" >{"Default", "UTF-8", "ANSI", "Unicode", "Unicode big Encode"}--0,1, 2, 3,4< /param> //<returns></returns> public static string ReadText (String filepath,int readencoding) { using (FileStream fsread = new FileStream (@FilePath, FileMode.Open)) { int fslen = ( int) fsread.length; byte[] Hebyte = new Byte[fslen]; int r = fsread.read (hebyte, 0, hebyte.length); String Mystr= ""; MyStr = System.Text.Encoding.Default.GetString (hebyte); return mystr; } }
2. Save: Write directly to the file
<span style= "White-space:pre" ></span>///<summary>// C # File stream write file, default append Filemode.append, But there's no additional append//</summary>// <param name= "text" > string to write </param>// <param Name= "FilePath" > File path + file name </param> public static void WriteText (String text,string FilePath) { byte[] MyByte = System.Text.Encoding.Default.GetBytes (Text); using (FileStream fswrite = new FileStream (@FilePath, FileMode.Create, FileAccess.Write)) { Fswrite.write ( MyByte, 0, mybyte.length);} ; }
3, Save: Operation with SaveFileDialog and FileStream
SaveFileDialog method
<span style= "White-space:pre" ></span> savefiledialog sfd = new SaveFileDialog (); SfD. Title = "Save as what?"; /SFD. InitialDirectory =filepath;//set Open path SFD. Filter = "txt files (*.txt) |*.txt|xls files (*.xls) |*.xls| All Files (*. *) |*.* ";//Set Optional file Save type SFD. FileName = fname;//set value file name sfd. DEFAULTEXT = "txt";//set value file suffix sfd. AddExtension = True;//<span style= "Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:13px; line-height:19.5px; " > Gets or sets a value that indicates whether the file dialog box automatically adds an extension </span> sfd to the filename if the user omits the extension. Restoredirectory = True;//set y or n remenber last file open path if (SFD). ShowDialog () = = DialogResult.OK) {string localfilepath = sfd. Filename.tostring ();//get the dialog save file path string savename= localfilepath.substring (LOCALFILEPATH.L Astindexof ("\ \") + 1);//get file save name tools. Filerw.writetext(TextBox1.Text, FilePath, savename);//write out}
File Save method
<span style= "White-space:pre" ></span>///<summary>//File Other write// </summary >// <param name= "text" > file contents to be written </param>// <param name= "FilePath" > File path </param > //<param name= "filename" > file name </param> public static void WriteText (string text, string FilePath, String FileName) { byte[] MyByte = System.Text.Encoding.Default.GetBytes (Text); string Buffername=filepath+filename; using (FileStream fswrite = new FileStream (@bufferName, FileMode.Create, FileAccess.Write)) { Fswrite.write (MyByte, 0, mybyte.length);} ; }
C # utility File Store