- 1 /// <summary>
- 2 /// 保存成文本
- 3 /// </summary>
- 4 /// <param name="sender"></param>
- 5 /// <param name="e"></param>
- 6 private void btnSaveTxt_Click(object sender, EventArgs e) 7 {
- 8 Dictionary < string,
- string > dicInfos = GetDictionaryInfos();
- 9 string filePath = "objPerson.txt";
- 10 //采用using关键字,会自动释放
- 11 using(FileStream fs = new FileStream(filePath, FileMode.Create)) 12 {
- 13 using(StreamWriter sw = new StreamWriter(fs, Encoding.Default)) 14 {
- 15 foreach(var keyPair in dicInfos) 16 {
- 17 sw.WriteLine(string.Format("{0}={1}", keyPair.Key, keyPair.Value));
- 18
- }
- 19
- }
- 20
- }
- 21
- }
- 22 23 /// <summary>
- 24 /// 从文本中读取
- 25 /// </summary>
- 26 /// <param name="sender"></param>
- 27 /// <param name="e"></param>
- 28 private void btnReadTxt_Click(object sender, EventArgs e) 29 {
- 30 string filePath = "objPerson.txt";
- 31 Dictionary < string,
- string > dic = new Dictionary < string,
- string > ();
- 32 //采用using关键字,会自动释放
- 33 using(FileStream fs = new FileStream(filePath, FileMode.Open)) 34 {
- 35 using(StreamReader sw = new StreamReader(fs, Encoding.Default)) 36 {
- 37
- while (!sw.EndOfStream) {
- 38 string lineInfo = sw.ReadLine();
- 39 dic.Add(lineInfo.Split(' = ')[0], lineInfo.Split(' = ')[1]);
- 40
- }
- 41
- }
- 42
- }
- 43 this.txtName.Text = dic["Name"];
- 44 this.dtBirthday.Text = dic["Birthday"];
- 45
- if (dic["Gender"] == this.rbBoy.Text) 46 {
- 47 this.rbBoy.Checked = true;
- 48
- }
- 49
- else {
- 50 this.rbGirl.Checked = true;
- 51
- }
- 52 string[] loves = dic["Love"].Split(' | ');
- 53 foreach(var love in loves) 54 {
- 55 foreach(var item in this.lsvLove.Items) 56 {
- 57 ListViewItem li = item as ListViewItem;
- 58
- if (li.Text == love) 59 {
- 60 li.Checked = true;
- 61
- }
- 62
- }
- 63
- }
- 64
- }
来源: