File.ReadAllText prevents Form from closing on x button click

438 Views Asked by At

I have a weird problem. I want to write the visible textBox.Text to an "ini" file on FormClosing (right before the form shuts down), so I double clicked that event under the main form's Properties panel and filled the associated function as follows:

    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);
    }

The form starts up without a problem, but it won't quit by clicking the x button. It only closes correctly when I comment the File.WriteAllText line out. If I just stop debugging, the files content doesn't change either.

EDIT :

The actual problem was the function which I used to find and return the userName from the ini file:

    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";
    }

The content of the ini file:

User Name = SomeName

Bot Name = SomeName

I copied the above function from stackoverflow. I was sure that it worked because it captured 'SomeName' just as I wanted. Now I use another function (also from stackoverflow), that searches the ini file for 'User Name = ' and returns the text that comes right after it.

    public static string GetTextAfterTextFromTextfile(string path, string textToSkip)
    {
        string str = File.ReadAllText(path);
        string result = str.Substring(str.IndexOf(textToSkip) + textToSkip.Length);
        return result;
    }

The problem is, that it returns

SomeNameBot Name = SomeName

Any hint on how to limit string result to only one line? Many thanks in advance!

1

There are 1 best solutions below

4
Hans Passant On

This is a normal mishap on the 64-bit version of Windows 7, caused by a nasty flaw in that operating system's Wow64 emulator. Not limited to Winforms apps, C++ and WPF apps are affected as well. For .NET apps, this only misbehaves if a debugger is attached. Repro code:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    throw new Exception("You will not see this");
}

The debugger won't stop when the exception is thrown and you can't close the window anymore. I wrote a more extensive answer about this problem, including recommended workarounds, in this post.

Quick fix in your case: use Debug + Exceptions, tick the Thrown checkbox. The debugger now stops when the exception is thrown, allowing you to diagnose and fix your bug.