c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
2010-05-18 08:58
453 查看
#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
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
相关文章推荐
- c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
- 【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
- winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建
- C#文件夹及文件操作(检查 创建 读取 写入 修改 删除)
- C# winform写入和读取TXT文件
- 用 C# 轻松读取、改变文件的创建、修改、访问时间
- 文件的创建,读取,写入,修改,删除
- c# winform读取xml文件创建菜单的代码
- 用 C# 轻松读取、改变文件的创建、修改、访问时间 .
- 用 C# 轻松读取、改变文件的创建、修改、访问时间
- C# winform 创建新文件--写入文本并另存为
- C# winform写入和读取TXT文件
- 用 C# 轻松读取、改变文件的创建、修改、访问时间
- C# 创建,读取,写入XML文件
- c# winform读取xml文件创建菜单的代码
- vs创建的c#winform项目出现缺少根文件,无法读取项目文件*.csproj?
- C# 轻松读取、改变文件的创建、修改、访问时间 z
- 文件的创建,读取,写入,修改,删除---python入门
- 用 C# 轻松读取、改变文件的创建、修改、访问时间
- C# 文件操作(读取文本/日志文件,读取文件列表,创建HTML,写入日志文件)