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 = 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://heisetoufa.iteye.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值