Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# StreamWriter有时会清除我的文件_C#_Streamwriter - Fatal编程技术网

C# StreamWriter有时会清除我的文件

C# StreamWriter有时会清除我的文件,c#,streamwriter,C#,Streamwriter,我正在制作一个程序来向Windows主机文件添加/删除条目。到目前为止,它工作得很好,除了有时使用我的表单删除一个项目时,它会清除整个hosts文件,而不会留下任何内容 这是我的密码: public void removeFromHosts(String key) { MessageBox.Show(key); if (key != "" || key != " ") { String[] strings = ne

我正在制作一个程序来向Windows主机文件添加/删除条目。到目前为止,它工作得很好,除了有时使用我的表单删除一个项目时,它会清除整个hosts文件,而不会留下任何内容

这是我的密码:

 public void removeFromHosts(String key)
    {
        MessageBox.Show(key);
        if (key != "" || key != " ")
        {
            String[] strings = new String[1000];

            String all = File.ReadAllText(hostFile);

            strings = all.Split('\n');


            StreamWriter writer = new StreamWriter(hostFile);

            foreach (String s in strings)
            {
                if (s != "255.255.255.255 " + key + " #Blocked by MyProgram")
                {
                    writer.Write(s);

                }
                else
                {
                    textBox1.Text += s;
                }
            }



            writer.Close();

            blockurls.SelectedItems[0].Remove();

            MessageBox.Show("URL Successfully removed");
        }
        else
        {
            MessageBox.Show("Empty");
        }
    }

我基本上是检查文本文件中的每一行,看看它是否包含url,如果它不包含url,那么将该行写入文件。是什么导致它擦除整个文件?

在我看来,您正在检查条目是否“包含”输入的值。然而,这将是危险的。在中,它声明如果向其传递空字符串,contains将始终返回true。如果键为空字符串,则将删除所有行。此外,如果您的密钥类似于“a”,则所有带有a的条目都将被删除。也许你应该检查字符串是否等于键,或者做一些更复杂的检查。我假设您将主机名作为键输入,将行分开,并确保主机名与键中的值匹配

您可能可以通过使用以下内容替换contains来修复代码

s = "255.255.255.255 " + key
或许

Regex.IsMatch(s,"^255\.255\.255\.255\s+" + Regex.Escape(key) + "\s*$")
使用此代码修复:

public void removeFromHosts(字符串键) {

            ArrayList strings = new ArrayList();

            StreamReader reader = new StreamReader(hostFile);

            while (reader.Peek() > -1)
            {
                strings.Add(reader.ReadLine());
            }


            reader.Close();

            StreamWriter writer = new StreamWriter(hostFile);

            foreach (String s in strings)
            {
                if (!s.Contains("255.255.255.255 " + key + " #Blocked by Parental Care"))
                {
                    writer.Write(s + "\n");

                }
            }



            writer.Close();

            blockurls.SelectedItems[0].Remove();

            MessageBox.Show("URL Successfully removed");

    }

请不要在标题前加上“C#”诸如此类。这就是标记的作用。我更新了包含检查,使其更具体一些。似乎删除了井,但有时仍然删除了所有条目。同样,如果键为空,它将删除开头为255.255.255.255的所有条目。在尝试之前,您可能希望检查该值是否为空。我检查了s是否为空=您可以在上面更新的代码中看到的完整字符串。您正在检查它是否包含,而不是是否等于。如果它仍在清空文件,可能最好使用调试器运行它,并逐行检查它,以查看是否有任何意外情况发生。我最好的猜测是,它在关闭f之前崩溃ile。导致您最终得到一个空文件。为什么不使用
file.ReadAllLines()
?这是我以前使用的。现在可以使用,我不想更改它。