Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# File.ReadAllText阻止窗体在单击x按钮时关闭_C#_Formclosing_Writealltext - Fatal编程技术网

C# File.ReadAllText阻止窗体在单击x按钮时关闭

C# File.ReadAllText阻止窗体在单击x按钮时关闭,c#,formclosing,writealltext,C#,Formclosing,Writealltext,我有个奇怪的问题。我想在FormClosing(就在表单关闭之前)将可见的textBox.Text写入一个“ini”文件,因此我双击了主表单属性面板下的该事件,并填充了相关函数,如下所示: private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // store the whole content in a string string settingsCo

我有个奇怪的问题。我想在FormClosing(就在表单关闭之前)将可见的textBox.Text写入一个“ini”文件,因此我双击了主表单属性面板下的该事件,并填充了相关函数,如下所示:

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        // store the whole content in a string
        string settingsContent = File.ReadAllText(settingsPath + "CBSettings");

        // replace a name with another name, which truly exists in the ini file 
        settingsContent.Replace(userName, userNameBox.Text);
        
        // write and save the altered content back to the ini file
        // settingsPath looks like this @"C:\pathToSettings\settingsFolder\"
        File.WriteAllText(settingsPath + "CBSettings", settingsContent);
    }
表单启动时没有问题,但单击x按钮不会退出。只有在注释File.WriteAllText行时,它才会正确关闭。如果我停止调试,文件内容也不会改变

编辑:

实际问题是我用来从ini文件中查找并返回用户名的函数:

    public static string GetTextAfterTextFromTextfile(string path, string file, string fileExtension, string textToLookFor)
    {
        string stringHolder;
        StreamReader sr = File.OpenText(path + file + fileExtension);
        while((stringHolder = sr.ReadLine()) != null)
        {
            if(stringHolder.Contains(textToLookFor))
            {
                return stringHolder.Replace(textToLookFor, "");
            }
        }
        sr.Close();
        return "Nothing found";
    }
ini文件的内容:

    public static string GetTextAfterTextFromTextfile(string path, string file, string fileExtension, string textToLookFor)
    {
        string stringHolder;
        StreamReader sr = File.OpenText(path + file + fileExtension);
        while((stringHolder = sr.ReadLine()) != null)
        {
            if(stringHolder.Contains(textToLookFor))
            {
                return stringHolder.Replace(textToLookFor, "");
            }
        }
        sr.Close();
        return "Nothing found";
    }
用户名=SomeName

Bot Name=SomeName

我从stackoverflow复制了上述函数。我确信它是有效的,因为它捕获了我想要的“某物名”。现在我使用另一个函数(同样来自stackoverflow),它在ini文件中搜索“User Name=”并返回紧跟其后的文本

    public static string GetTextAfterTextFromTextfile(string path, string textToSkip)
    {
        string str = File.ReadAllText(path);
        string result = str.Substring(str.IndexOf(textToSkip) + textToSkip.Length);
        return result;
    }
问题是,它又回来了

SomeNameBot Name=SomeName


关于如何将
字符串结果
限制为仅一行的提示?非常感谢

这在64位版本的Windows 7上是一个正常的灾难,由该操作系统的Wow64仿真器中的严重缺陷引起。不限于WiFrm应用程序,C++和WPF应用程序也受到影响。对于.NET应用程序,只有在附加了调试器的情况下才会出现错误。复制代码:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    throw new Exception("You will not see this");
}
当抛出异常并且您无法再关闭窗口时,调试器将不会停止。关于这个问题,我在中写了一个更广泛的答案,包括推荐的解决方法


快速修复:使用Debug+异常,勾选抛出复选框。当抛出异常时,调试器现在停止,允许您诊断和修复错误。

您确定它没有抛出异常吗?例如,您似乎试图保存到文件夹而不是文件?我在您的文件名中没有看到扩展名。顺便说一句,查看try/catch。然后,您可能会收到有意义的错误消息,而不是冻结的窗口。不,根本没有错误,而且它没有冻结,它只是对x按钮的单击没有反应,因此不会保存文件中的任何内容。请尝试将新替换的内容存储在单独的字符串中,因为字符串是不可变的。根据您当前的编码,替换的内容将不会反映在新文件中。我尝试改用StreamWriter,但它也不起作用,因为行“StreamWriter.Write(path)”。我真的不知道该怎么办。呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜。我更新了我的问题。当然,一旦你知道如何恢复调试器,那么找到真正的bug就很简单了,不是吗?你否决了这篇文章吗?那不是很愚蠢吗?不,汉斯,我没有否决你的帖子,事实上我试图否决它,但我的声誉分数太低了。我甚至没有恢复调试器,我只是改变了文件的读取方式,但仍然没有成功。