【转】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函数可以向指定的文 ...
随机推荐
- JavaScript设模式---单例模式
单例模式也称为单体模式,其中: 1,单体模式用于创建命名空间,将系列关联的属性和方法组织成一个逻辑单元,减少全局变量. 逻辑单元中的代码通过单一的变量进行访问. 2,三个特点: ① 该类只有一个实例: ...
- python 基础 列表 小例子
存主机ip到列表 host_list=[] netip='192.168.1' for hostip in range(1,254): ip = netip +str(hostip) host_lis ...
- javadoc 工具生成开发API文档
=====================先来一点成就感===================== package com.springMybatis.dao; import com.springMy ...
- ViewPage+Fragment(仿微信切换带通知)
第一步 : 布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...
- 【总结整理】overflow: auto/hidden;清除自己
.top-nav{ font-size: 12px; font-weight: bold; list-style-type: none; border-bottom: 8px solid #DC4E1 ...
- Assembly.GetManifestResourceNames()获取不到资源文件
Assembly.GetManifestResourceNames()获取到的是嵌入的资源文件 右键资源文件属性 将生成操作改为嵌入的资源就OK咯
- Material使用07 MdGridListModule的使用
1 MatGridListModule简介 对相似数据的展现,尤其是像是图片的展示 使用起来很像表格 官方文档:点击前往 2 MatGridListModule提供的指令 2.1 mat-grid-l ...
- ARC097D Equals
传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...
- 1. sqlmap超详细笔记+思维导图
sqlmap思维导图: 基本操作笔记: -u #注入点 -f #指纹判别数据库类型 -b #获取数据库版本信息 -p #指定可测试的参数(?page=1&id=2 -p "page, ...
- 项目接入apm后错误报警总结
此文已由作者张磊授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言: 后端服务一般都有监控措施,一般可以及时发现线上错误,但是很多项目的前端却没有线上报警服务,即使有错误, ...