winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建
来源:互联网 发布:easypangel mysql目录 编辑:程序博客网 时间:2024/06/05 10:32
创建文件和读取文件的值
- #region 判断文件是否存在,不存在则创建,否则读取值显示到窗体
- public FormMain()
- {
- InitializeComponent();
- //ReadFile(Application.StartupPath + "\\AlarmSet.txt");
- //也是判断文件是否存在
- //System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(Application.StartupPath + "\\AlarmSet.txt");
- //MessageBox.Show(info.Exists.ToString());
- //MessageBox.Show(Application.StartupPath + "\\AlarmSet.txt");
- //判断文件是否存在
- if (!File.Exists(Application.StartupPath + "\\AlarmSet.txt"))
- {
- //File.Create(Application.StartupPath + "\\AlarmSet.txt");//创建该文件
- FileStream fs1 = new FileStream(Application.StartupPath + "\\AlarmSet.txt", FileMode.Create, FileAccess.Write);//创建写入文件
- StreamWriter sw = new StreamWriter(fs1);
- sw.WriteLine("[runtype]");//开始写入值
- sw.WriteLine("type=1");
- sw.WriteLine("\r\n");
- sw.WriteLine("--报警设置 PPWS 号牌匹配位数 PPWZ 匹配位置 0前匹配 1后匹配");
- sw.WriteLine("[Alarm]");
- sw.WriteLine("PPWZ=0");
- sw.WriteLine("PPWS=8");
- sw.WriteLine("\r\n");
- sw.WriteLine("[Server]");
- sw.WriteLine("ListenPort=2005");
- sw.WriteLine("\r\n");
- sw.WriteLine("[Form]");
- sw.WriteLine("PPWZ=0");
- sw.Close();
- fs1.Close();
- }
- //读取文件值并显示到窗体
- FileStream fs = new FileStream(Application.StartupPath + "\\AlarmSet.txt", FileMode.Open, FileAccess.ReadWrite);
- StreamReader sr = new StreamReader(fs);
- string line = sr.ReadLine();
- int curLine = 0;
- while (line != null)
- {
- if (++curLine == 7 && line.Equals("PPWZ=0"))//文件第7行并且值为PPWZ=0的时候设置单选钮选中前匹配
- {
- radioButton1.Checked = true;
- radioButton2.Checked = false;
- //MessageBox.Show("前");
- }
- else if (curLine == 8 && line.Equals("PPWZ=1"))//文件第8行并且值为PPWZ=1的时候设置单选钮选中后匹配
- {
- radioButton2.Checked = true;
- radioButton1.Checked = false;
- //MessageBox.Show("后");
- }
- if (curLine == 8)//文件第8行
- {
- textBox1.Text = line.Substring(line.LastIndexOf("=") + 1);//截取=号后边的值
- }
- //MessageBox.Show("第" + (++curLine).ToString() + "行: " + line);
- //Console.WriteLine("第" + (++curLine).ToString() + "行: " + line);
- line = sr.ReadLine();
- }
- sr.Close();
- fs.Close();
- }
- #endregion
- 修改文件的值
- #region 保存设置 按钮 按下
- private void button6_Click(object sender, EventArgs e)
- {
- if(radioButton1.Checked == true )
- {
- EditFile(7, "PPWZ=0", Application.StartupPath + "\\AlarmSet.txt");
- EditFile(8, "PPWS=" + textBox1.Text, Application.StartupPath + "\\AlarmSet.txt");
- }
- if (radioButton2.Checked == true)
- {
- EditFile(7, "PPWZ=1", Application.StartupPath + "\\AlarmSet.txt");
- EditFile(8, "PPWS=" + textBox1.Text, Application.StartupPath + "\\AlarmSet.txt");
- }
- }
- #endregion
- #region 设置匹配
- public static void EditFile(int curLine, string newLineValue, string patch)
- {
- FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);
- StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));//解决写入文件乱码
- string line = sr.ReadLine();
- StringBuilder sb = new StringBuilder();
- for (int i = 1; line != null; i++)
- {
- sb.Append(line + "\r\n");
- if (i != curLine - 1)
- line = sr.ReadLine();
- else
- {
- sr.ReadLine();
- line = newLineValue;
- }
- }
- sr.Close();
- fs.Close();
- FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);
- StreamWriter sw = new StreamWriter(fs1);
- sw.Write(sb.ToString());
- sw.Close();
- fs.Close();
- }
- #endregion
- 原创地址http://www.cnblogs.com/aflyfly/archive/2009/10/10/1580155.html
0 0
- winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建
- c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
- 文件的创建,读取,写入,修改,删除
- 文件的创建、写入
- 浅谈文件的创建、读取及写入
- 文件的创建 读取 写入知识点
- 关于python文件的读取创建写入
- 文件创建读取写入
- 日志文件的写入以及文件创建
- 文件的创建及写入
- INI 文件的创建/修改/读取
- 对文件进行创建、写入、读取
- 对文件进行创建、写入、读取
- winform中 如何读取Excel文件,之后再把它写入一个新的Excel文件
- 文件的读取写入
- 文件的创建写入读取 日期控件的使用
- 把RAR文件伪装成图片(文件的读取写入)
- 文件的创建于读取
- cocos2d 游戏开发的常用工具汇总(ios的也先收着)
- 如何计算时间复杂度
- android 程序间跳转
- Oracle 数组
- WPF 用ShowDialog的方法显示窗口的返回值
- winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建
- ORA-12541:TNS:无监听程序 解决办法
- POJ 2226 Muddy Fields(匈牙利算法—最小点覆盖)
- c语言实现二叉树数据结构
- 自动结算/应用/初始化
- 我也来开发2048之终极奥义
- Android工程调用第三方动态链接库的方法(静态库同理)
- 注册(六)之有效期过小
- QPBOC之GPO(二):脱机货币检查