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("/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  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C# WinForm上传文件的示例代码: ```csharp private void btnUpload_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; string url = "http://localhost/UploadFileWeb/WebForm1.aspx"; string paramName = "file"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); request.ContentType = "multipart/form-data; boundary=" + boundary; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string header = string.Format(headerTemplate, paramName, Path.GetFileName(filePath)); byte[] headerBytes = Encoding.UTF8.GetBytes(header); requestStream.Write(headerBytes, 0, headerBytes.Length); using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } } byte[] trailerBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailerBytes, 0, trailerBytes.Length); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseText = reader.ReadToEnd(); MessageBox.Show(responseText); } } } } ``` 该示例代码使用HttpWebRequest类向指定的URL上传文件。在上传文件之前,需要设置请求的Method为POST,ContentType为multipart/form-data,并设置请求头部的边界(boundary)。然后,将文件内容写入请求流中,最后发送请求并获取响应。在获取响应后,可以从响应流中读取服务器返回的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值