简体   繁体   中英

C# - Settings.Default.Save() is very slow

I want to save some UI changes when the program closes. I've read that one of the simplest ways to do that is with settings. I figured I'd save those changes to the settings whenever the program closes, instead of saving it everytime the user changes those settings. So, I have this:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    { 
        Properties.Settings.Default.PostProcessSubs = checkBox_PPSubs.Checked;
        Properties.Settings.Default.Save();
    }

Which just saves whether that checkbox is on or off by default. However, when I have this, the application hangs for a second or two when I press X to close it. If I comment out the save() command, it closes ok, but of course the changes aren't saved. Besides this, everything works ok, so I find it quite strange that this process is so slow.

BTW, my settings aren't bloated or anything, I only have two booleans and two small strings in there.

Edit: This only happens when run trough Start Without Debugging. Running trough debug or trough release works OK. Quite strange still.

Edit2: no,still happens. Slow as hell.

Ok,I solved this and I feel retarded. But here's a pointer if someone has a similar problem. So, what was happening was my config file was 16 MB. Why? Well, one of the configs was the path of an executable. However, the code I used for that executable was not correct, as I had copied it without thinking from other part of my program. I had this... it's sad

 Properties.Settings.Default.ProgramPath = 
System.IO.File.ReadAllText(openFileDialog1.FileName.ToString());

When obviously it should have just been

 Properties.Settings.Default.ProgramPath = 
openFileDialog1.FileName.ToString();

Of course, the original code I had was converting the entire executable into text, and saving it into the config file. The config file got bloated, and even if I didn't use that part of the code anymore, the config file was already bloated. So, whenever I saved any other configuration, instead of saving a few KB file, it was saving a 16 MB file. Everytime. So, yeah...

TLDR: Check your config file size... might be bloated for some reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM