c# winform 建立檔案,把值寫入檔案,讀取檔案裡的值,修改檔案的值,對檔案的建立,寫入,修改

來源:互聯網
上載者:User
  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  

修改檔案的值

  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 + "/r/n");   
  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  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.