简体   繁体   English

C#-Settings.Default.Save()非常慢

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

I want to save some UI changes when the program closes. 我想在程序关闭时保存一些UI更改。 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. 但是,当我遇到这个问题时,按X将其关闭时,应用程序会挂一两秒钟。 If I comment out the save() command, it closes ok, but of course the changes aren't saved. 如果我注释掉save()命令,它会关闭,但是当然不会保存更改。 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. 顺便说一句,我的设置没有ated肿,什么都没有,我只有两个布尔值和两个小字符串。

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. Edit2:不,仍然发生。 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. 因此,发生的是我的配置文件为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. 因此,每当我保存任何其他配置时,它都保存了16 MB文件,而不是保存几个KB文件。 Everytime. 每次。 So, yeah... 是的...

TLDR: Check your config file size... might be bloated for some reason. TLDR:检查您的配置文件大小...由于某些原因可能会might肿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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