【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改

创建文件和读取文件的值

 1 #region 判断文件是否存在,不存在则创建,否则读取值显示到窗体
 2 
 3 public FormMain()
 4 {
 5     InitializeComponent();
 6 
 7     //ReadFile(Application.StartupPath + "\AlarmSet.txt");
 8 
 9     //也是判断文件是否存在
10     //System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(Application.StartupPath + "\AlarmSet.txt");
11     //MessageBox.Show(info.Exists.ToString());
12             
13     //MessageBox.Show(Application.StartupPath + "\AlarmSet.txt");
14 
15     //判断文件是否存在
16     if (!File.Exists(Application.StartupPath + "\AlarmSet.txt"))
17     {
18         //File.Create(Application.StartupPath + "\AlarmSet.txt");//创建该文件
19 
20         FileStream fs1 = new FileStream(Application.StartupPath + "\AlarmSet.txt", FileMode.Create, FileAccess.Write);//创建写入文件 
21         StreamWriter sw = new StreamWriter(fs1);
22         sw.WriteLine("[runtype]");//开始写入值
23         sw.WriteLine("type=1");
24 
25         sw.WriteLine("
");
26                 
27         sw.WriteLine("--报警设置 PPWS 号牌匹配位数 PPWZ 匹配位置 0前匹配 1后匹配");
28         sw.WriteLine("[Alarm]");
29         sw.WriteLine("PPWZ=0");
30         sw.WriteLine("PPWS=8");
31 
32         sw.WriteLine("
");
33 
34         sw.WriteLine("[Server]");
35         sw.WriteLine("ListenPort=2005");
36 
37         sw.WriteLine("
");
38 
39         sw.WriteLine("[Form]");
40         sw.WriteLine("PPWZ=0");
41 
42         sw.Close();
43         fs1.Close();
44 
45     }
46 
47     //读取文件值并显示到窗体 
48     FileStream fs = new FileStream(Application.StartupPath + "\AlarmSet.txt", FileMode.Open, FileAccess.ReadWrite);
49     StreamReader sr = new StreamReader(fs);
50     string line = sr.ReadLine();
51     int curLine = 0;
52     while (line != null)
53     {
54         if (++curLine == 7 && line.Equals("PPWZ=0"))//文件第7行并且值为PPWZ=0的时候设置单选钮选中前匹配
55         {
56             radioButton1.Checked = true;
57             radioButton2.Checked = false;
58             //MessageBox.Show("前");    
59         }
60         else if (curLine == 8 && line.Equals("PPWZ=1"))//文件第8行并且值为PPWZ=1的时候设置单选钮选中后匹配
61         {
62             radioButton2.Checked = true;
63             radioButton1.Checked = false;
64             //MessageBox.Show("后");
65         }
66 
67         if (curLine == 8)//文件第8行
68         {
69             textBox1.Text = line.Substring(line.LastIndexOf("=") + 1);//截取=号后边的值
70         }
71 
72         //MessageBox.Show("第" + (++curLine).ToString() + "行:   " + line);
73         //Console.WriteLine("第" + (++curLine).ToString() + "行:   " + line);
74         line = sr.ReadLine();
75     }
76     sr.Close();
77     fs.Close();
78 }
79 
80 #endregion

修改文件的值 

C#代码
 1 #region 保存设置 按钮 按下
 2 
 3 private void button6_Click(object sender, EventArgs e)
 4 {
 5     if(radioButton1.Checked == true )
 6     {
 7         EditFile(7, "PPWZ=0", Application.StartupPath + "\AlarmSet.txt");
 8         EditFile(8, "PPWS=" + textBox1.Text, Application.StartupPath + "\AlarmSet.txt");
 9     }
10 
11     if (radioButton2.Checked == true)
12     {
13         EditFile(7, "PPWZ=1", Application.StartupPath + "\AlarmSet.txt");
14         EditFile(8, "PPWS=" + textBox1.Text, Application.StartupPath + "\AlarmSet.txt");
15     }
16 }
17 
18 #endregion
19 
20 
21 #region 设置匹配
22 
23 public static void EditFile(int curLine, string newLineValue, string patch)
24 {
25     FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);
26     StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));//解决写入文件乱码
27     string line = sr.ReadLine();
28     StringBuilder sb = new StringBuilder();
29     for (int i = 1; line != null; i++)
30     {
31         sb.Append(line + "
");
32         if (i != curLine - 1)
33             line = sr.ReadLine();
34         else
35         {
36             sr.ReadLine();
37             line = newLineValue;
38         }
39     }
40     sr.Close();
41     fs.Close();
42     FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);
43     StreamWriter sw = new StreamWriter(fs1);
44     sw.Write(sb.ToString());
45     sw.Close();
46     fs.Close();
47 }
48 
49 #endregion
原文地址:https://www.cnblogs.com/gates/p/3549054.html