【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
创建文件和读取文件的值
#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 = ;
while (line != null)
{
if (++curLine == && line.Equals("PPWZ=0"))//文件第7行并且值为PPWZ=0的时候设置单选钮选中前匹配
{
radioButton1.Checked = true;
radioButton2.Checked = false;
//MessageBox.Show("前");
}
else if (curLine == && line.Equals("PPWZ=1"))//文件第8行并且值为PPWZ=1的时候设置单选钮选中后匹配
{
radioButton2.Checked = true;
radioButton1.Checked = false;
//MessageBox.Show("后");
} if (curLine == )//文件第8行
{
textBox1.Text = line.Substring(line.LastIndexOf("=") + );//截取=号后边的值
} //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(, "PPWZ=0", Application.StartupPath + "\\AlarmSet.txt");
EditFile(, "PPWS=" + textBox1.Text, Application.StartupPath + "\\AlarmSet.txt");
} if (radioButton2.Checked == true)
{
EditFile(, "PPWZ=1", Application.StartupPath + "\\AlarmSet.txt");
EditFile(, "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 = ; line != null; i++)
{
sb.Append(line + "\r\n");
if (i != curLine - )
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
【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改的更多相关文章
- java处理Excel文件---excel文件的创建,删除,写入,读取
这篇文章的代码是我封装的excel处理类,包含推断excel是否存在,表格索引是否存在,创建excel文件,删除excel文件,往excel中写入信息,从excel中读取数据. 尤其在写入与读取两个方 ...
- js创建、写入、读取文件(转)
下面是对此知识的系统介绍(转自互联网): Javascript 是网页制作中离不开的脚本语言,依靠它,一个网页的内容才生动活泼.富有朝气.但也许你还没有发现并应用它的一些更高级的功能吧?比如,对文件和 ...
- 在C#程序中,创建、写入、读取XML文件的方法
一.在C#程序中,创建.写入.读取XML文件的方法 1.创建和读取XML文件的方法,Values为需要写入的值 private void WriteXML(string Values) { //保存的 ...
- C# winform写入和读取TXT文件
C# winform写入和读取TXT文件 string str; str=this.textBox1.Text; StreamWriter sw = new ...
- winform 写入和读取TXT文件
C# winform写入和读取TXT文件 string str; str=this.textBox1.Text; StreamWriter sw = new StreamWriter(Applicat ...
- UWP入门(十)--创建、写入和读取文件
原文:UWP入门(十)--创建.写入和读取文件 核心的 API github代码 StorageFolder 类 StorageFile 类 FileIO 类 使用 StorageFile 对象读取和 ...
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...
- 【PHP】文件写入和读取详解
文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...
- PHP 文件写入和读取(必看篇)
文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...
- Win10系列:JavaScript写入和读取文件
正如上面的内容中所提到的,文件保存选取器用于保存文件,通过Windows.Storage.Pickers命名空间中的FileSavePicker类的pickSaveFileAsync函数可以向指定的文 ...
随机推荐
- PWA PSI statusingclient.UpdateStatus更新任务页面的AssnCustomFields的TextValue值
1.注意Changesxml格式和下面一定要一样 2.CustomFieldGuid和CustomFieldName都不能少,自定义域的uid和name其中uid或者是MD_PROP_UID_SECO ...
- 浏览器怎么禁用和开启Javascript
转自;http://360.bgu.edu.cn/help/openJsHelp.html IE内核的浏览器禁用和启用Javascript功能都类似,首先我们需要打开IE8浏览器. 之后点击其右上角的 ...
- 伪分布模式 hive查询
[root@node1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian ...
- sort,uniq,cut,wc命令详解
sortsort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 s ...
- day18-事务与连接池 4.事务特性
- assert.fail()
assert.fail(message) assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) oper ...
- Add lombok to IntelliJ IDEA
Lombok study link: https://www.jianshu.com/p/365ea41b3573 Add below dependency code to pom.xml <d ...
- 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)
- java之格式化输出
参考http://how2j.cn/k/number-string/number-string-foramt/320.html#nowhere 格式化输出 如果不使用格式化输出,就需要进行字符串连接, ...
- ibatis知识点汇总
一个参数,返回Map <select id="getShopInfo" parameterClass="java.lang.String" resultC ...