winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建

来源:互联网 发布:easypangel mysql目录 编辑:程序博客网 时间:2024/06/05 10:32




创建文件和读取文件的值

  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("\r\n");   
  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("\r\n");   
  33.   
  34.         sw.WriteLine("[Server]");   
  35.         sw.WriteLine("ListenPort=2005");   
  36.   
  37.         sw.WriteLine("\r\n");   
  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  
  81. 修改文件的值
  82. #region 保存设置 按钮 按下   
  83.   
  84. private void button6_Click(object sender, EventArgs e)   
  85. {   
  86.     if(radioButton1.Checked == true )   
  87.     {   
  88.         EditFile(7, "PPWZ=0", Application.StartupPath + "\\AlarmSet.txt");   
  89.         EditFile(8, "PPWS=" + textBox1.Text, Application.StartupPath + "\\AlarmSet.txt");   
  90.     }   
  91.   
  92.     if (radioButton2.Checked == true)   
  93.     {   
  94.         EditFile(7, "PPWZ=1", Application.StartupPath + "\\AlarmSet.txt");   
  95.         EditFile(8, "PPWS=" + textBox1.Text, Application.StartupPath + "\\AlarmSet.txt");   
  96.     }   
  97. }  
  98.  
  99. #endregion  
  100.  
  101.  
  102. #region 设置匹配   
  103.   
  104. public static void EditFile(int curLine, string newLineValue, string patch)   
  105. {   
  106.     FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);   
  107.     StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));//解决写入文件乱码  
  108.     string line = sr.ReadLine();   
  109.     StringBuilder sb = new StringBuilder();   
  110.     for (int i = 1; line != null; i++)   
  111.     {   
  112.         sb.Append(line + "\r\n");   
  113.         if (i != curLine - 1)   
  114.             line = sr.ReadLine();   
  115.         else  
  116.         {   
  117.             sr.ReadLine();   
  118.             line = newLineValue;   
  119.         }   
  120.     }   
  121.     sr.Close();   
  122.     fs.Close();   
  123.     FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);   
  124.     StreamWriter sw = new StreamWriter(fs1);   
  125.     sw.Write(sb.ToString());   
  126.     sw.Close();   
  127.     fs.Close();   
  128. }  
  129.  
  130. #endregion 
  131. 原创地址http://www.cnblogs.com/aflyfly/archive/2009/10/10/1580155.html
0 0
原创粉丝点击