This is an actual case of the serialization article in the previous article. The main interface of the winform program is as follows:
Idea: When you click the Save button to save the title and content to the set, it is assumed that it is a dictionary <K, V>, in addition, this set can avoid generating two diaries with the same title. The data added to the set is serialized in binary format to a file. In the program execution directory, create a "Notes" folder to store the log file. The file name of the log file is named by the title. When a program is loaded, if there are files in the "Notes" folder, load the files in the list on the right. When you click "deserialize", the files are displayed on the left. The code below is as follows:
Define a set under the form1 class:
1 Dictionary<string, string> dic = new Dictionary<string, string>();
The "save" button click operation code is as follows:
1 var stitle = txttitle. text. trim (); 2 var scontent = txtcontent. text. trim (); 3 4 dic. add (stitle, scontent); 5 6 string fullpath = path. combine ("Notes", stitle + ". TXT "); 7 using (filestream fswrite = new filestream (fullpath, filemode. create, fileaccess. write) 8 {9 // perform binary serialization 10 binaryformatter BF = new binaryformatter (); 11 BF. serialize (fswrite, DIC); 12 MessageBox. show ("successfully added a diary"); 13}
The event processing code in form1_load is as follows:
1 string sPath1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 2 string sPath2 = "Notes"; 3 string fullPath1 = Path.Combine(sPath1, sPath2); 4 string[] strFiles = Directory.GetFiles(fullPath1, "*.txt"); 5 6 if (strFiles.Length>0) 7 { 8 foreach (string item in strFiles) 9 {10 var s = Path.GetFileName(item);11 listBox1.Items.Add(s);12 } 13 }
The event processing code in listbox1_selectedvaluechanged is as follows:
1 If (listbox1.selecteditem! = NULL) 2 {3 var filename = listbox1.selecteditem. tostring (); 4 var fullpath2 = path. combine ("Notes", filename); 5 using (filestream fsread = new filestream (fullpath2, filemode. open, fileaccess. read) 6 {7 binaryformatter bf1 = new binaryformatter (); 8 // perform binary deserialization 9 dictionary <string, string> dic2 = bf1.deserialize (fsread) as Dictionary <string, string>; 10 foreach (keyvaluepair <string, string> item in dic2) 11 {12 txttitle. TEXT = item. key; 13 txtcontent. TEXT = item. value; 14} 15} 16} 17 else18 {19 MessageBox. show ("select the file to view"); 20}
Summary:
There are still some bugs in the program, because the main focus is on the use of serialization. If you have time to improve the function in the future.
1. For newly added diaries, they need to be loaded to the ListBox on the right in the next program startup (the idea is to save the logs and load them to ListBox );
2. for the modification of the diary, after modifying the content, click Save to view the modification changes in due time. If you modify both the title and content, the program considers it as a brand new diary (the dictionary set will be re-added due to different keys ).