C# 读写文件window form操作代码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

C# 读写文件window form操作代码

///////////创建文件和读取文件的值
  
  
#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("rn");
                  
        sw.WriteLine("--报警设置 PPWS 号牌匹配位数 PPWZ 匹配位置 0前匹配 1后匹配");
        sw.WriteLine("[Alarm]");
        sw.WriteLine("PPWZ=0");
        sw.WriteLine("PPWS=8");
  
        sw.WriteLine("rn");
  
        sw.WriteLine("[Server]");
        sw.WriteLine("ListenPort=2005");
  
        sw.WriteLine("rn");
  
        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 + "rn");
        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

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C#抓取网页的几种方法

下一篇:插入排序算法的C#实现