Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将复选框的状态保存到文件C的特定行#_C#_Checkbox_Save - Fatal编程技术网

C# 将复选框的状态保存到文件C的特定行#

C# 将复选框的状态保存到文件C的特定行#,c#,checkbox,save,C#,Checkbox,Save,我已经搜索了多个解决方案,但找不到具体解决我问题的解决方案: 我想完成的是将复选框的状态保存到文件的特定行。 我使用了相同的代码从openFileDialog保存文件补丁 if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var lines = File.ReadAllLines("patcher.conf"); lines[0] = openFileDialog1.FileName; File

我已经搜索了多个解决方案,但找不到具体解决我问题的解决方案:

我想完成的是将复选框的状态保存到文件的特定行。 我使用了相同的代码从openFileDialog保存文件补丁

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var lines = File.ReadAllLines("patcher.conf");
lines[0] = openFileDialog1.FileName;
File.WriteAllLines("patcher.conf", lines);
}
上面的代码将文件补丁保存在文本文件的第一(0索引)行中,它可以工作! 但出于某种原因,当我尝试在以下方面做同样的事情时:

private void checkexe_CheckedChanged(object sender, EventArgs e)
    {
        string line;
        System.IO.StreamReader file =
           new System.IO.StreamReader("patcher.conf");
        while ((line = file.ReadLine()) != null)
        {
            var lines = File.ReadAllLines("patcher.conf");
            lines[1] = checkexe.Checked.ToString();
            File.WriteAllLines("patcher.conf", lines);
        }
        file.Close();
    }
并将有关复选框状态的信息保存在第2行(文件的1个索引行)中,错误显示: 进程无法访问该文件,因为其他进程正在使用该文件。
我做错了什么?

在文件流上,您使用了readwrite

System.IO.FileStream fs=new System.IO.FileStream(txtFilePath.Text、System.IO.FileMode.Open、System.IO.FileAccess.Read、System.IO.FileShare.ReadWrite)


System.IO.StreamReader sr=新的System.IO.StreamReader(fs)

您写入文件的方法有缺陷。您正在打开文件并读取所有行,但对于每一行,您将再次读取所有行并将文件保存在同一循环中。这将导致您的
进程无法访问该文件,因为另一个进程正在使用该文件

private void checkexe_CheckedChanged(object sender, EventArgs e)
{
    string line;
    System.IO.StreamReader file = new System.IO.StreamReader("patcher.conf");
    while ((line = file.ReadLine()) != null)
    {
        var lines = File.ReadAllLines("patcher.conf");
        lines[1] = checkexe.Checked.ToString();
        File.WriteAllLines("patcher.conf", lines);
    }
    file.Close();
}
相反,请尝试以下内容:(未经测试,但应该会让您朝着正确的方向前进)

private void checkexe\u CheckedChanged(对象发送方,事件参数e)
{
var lines=File.ReadAllLines(“patcher.conf”);
对于(变量i=0;i
我有点困惑-我应该以某种方式在代码中使用它吗?我应该把它放在哪里?我如何用这个保存到文本文件的特定行?这就是你所有的代码吗?错误是它无法写入该文件,因为另一个文件已将其锁定以进行写入。您正在使用StreamReader吗?刚刚更新了代码-在这里您可以看到StreamReader。(以前所有的流读取器都是file.Close()-ed)编辑:好的,我想答案让我吃惊了-是否可能事件:checkeexe\u CheckedChanged在表单加载过程中激发?工作起来很有魅力。现在我更清楚地看到了应该如何做——谢谢。
private void checkexe_CheckedChanged(object sender, EventArgs e)
{
    var lines = File.ReadAllLines("patcher.conf");
    for(var i = 0; i < lines.Length; i++)
    {
        if (i == 1)
            lines[i] = checkexe.Checked.ToString();
    }
    File.WriteAllLines("patcher.conf", lines);
}