可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has answers here:
Closed 2 years ago.
this bug is pretty unusual. Basically my code will change the Settings.Default.Example
then save and restart the program. Then when it loads, it shows a message box. However oddly, it shows a empty value when the form loads.
Here is my code:
Main.cs
private void Button1_Click(object sender, EventArgs e)
{
Settings.Default.Example = "Somevalue"; //Sets a value to the settings
Settings.Default.Save(); // Save it
MessageBox.Show(Settings.Default.Example); //Confirming it has been saved
Application.Restart();
}
private void Main_Load(object sender, EventArgs e)
{
MessageBox.Show(Settings.Default.Example); // Here is the weird part, it shows empty.
}
The MessageBox
will show "Somevalue" when the button was clicked then the applcation restarts and the MessageBox
that showed was empty. However repeating the process by clicking the button once more and restarting it does show the "Somevalue" MessageBox
. Please help! Many Thanks!
回答1:
Maybe you ran the same mistake as I did: setting the setting's scope
to Application
. Those kind of settings are not saved.
Set it to User
to solve the problem.
回答2:
After a full day of researching and studying the subject, I was able to solve this by putting the Configuration to the user:
Using System.Configuration;
Properties.Settings.Default.strinconn = txt_stringconn.Text;
Properties.Settings.Default.Save ();
Properties.Settings.Default.Upgrade ();
MessageBox.Show ("Saved Settings");
Application.Restart ();
回答3:
rene is correct - you need to call Default.Reload
after calling the Save
method:
Settings.Default.Save();
Settings.Default.Reload();
Possibly a bug - ?
Posting as a reply to increase visibility -
回答4:
If your AssemblyInfo.cs
file has a *
in Assembly Version then it's refreshing the file every build so you won't see persistence or reliability until you change that to a hard number and rebuild all, then retest everything.
回答5:
Beware of calling
Settings.Default.Reload();
after each
Settings.Default.Save();
Save() function actually saves your changes to the file but it is not reflected to your running code. Thus, your code keeps the copy of the previous version of the file.
When you call Save() at another location in your code, it writes over your first change, effectively reverting your first change back to original value.
Very hard to pin down even when debugging.
回答6:
Please have a look at this Question
Especially, try out the following code from an answer there:
using System.Configuration; // Add a reference to System.Configuration.dll
...
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
Also, check out this overview, perhaps you ran into some limitation which is not easy to tell from your question.
This codeproject article might be of some help.
回答7:
did you try calling ConfigurationManager.RefreshSection
before checking that it got saved ad you could try it again after the reload
回答8:
If you need to test your application how actually working in this case better to run the exe file. When you run on visual studio on debug mode when those settings saving it will take some time. Go to debug folder and run the exe you will get the messages as expected.
回答9:
Using Visual Studio 2013 - there is nothing I could do to make it work reliably, I would call Save and it did not save.
Save and then immediately Reload and it still would not retain the values on subsequent runs (probably related to when I stopped debugging could not identify the root cause) - very frustrating, likely there is an underlying bug but I cannot prove it.
To avoid getting crazy with this I decided to use the registry - the most fundamental way to keep app settings for an app.
Recommended for you all. Here is the code:
public static class RegistrySettings
{
private static RegistryKey baseRegistryKey = Registry.CurrentUser;
private static string _SubKey = string.Empty;
public static string SubRoot
{
set
{ _SubKey = value; }
}
public static string Read(string KeyName, string DefaultValue)
{
// Opening the registry key
RegistryKey rk = baseRegistryKey;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(_SubKey);
// If the RegistrySubKey doesn't exist return default value
if (sk1 == null)
{
return DefaultValue;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName);
}
catch (Exception e)
{
ShowErrorMessage(e, String.Format("Reading registry {0}", KeyName.ToUpper()));
return null;
}
}
}
public static bool Write(string KeyName, object Value)
{
try
{
// Setting
RegistryKey rk = baseRegistryKey;
// I have to use CreateSubKey
// (create or open it if already exits),
// 'cause OpenSubKey open a subKey as read-only
RegistryKey sk1 = rk.CreateSubKey(_SubKey);
// Save the value
sk1.SetValue(KeyName, Value);
return true;
}
catch (Exception e)
{
ShowErrorMessage(e, String.Format("Writing registry {0}", KeyName.ToUpper()));
return false;
}
}
private static void ShowErrorMessage(Exception e, string Title)
{
if (ShowError == true)
MessageBox.Show(e.Message,
Title
, MessageBoxButtons.OK
, MessageBoxIcon.Error);
}
}
Usage:
private void LoadDefaults()
{
RegistrySettings.SubRoot = "Software\\Company\\App";
textBoxInputFile.Text = RegistrySettings.Read("InputFileName");
}
private void SaveDefaults()
{
RegistrySettings.SubRoot = "Software\\Company\\App";
RegistrySettings.Write("InputFileName", textBoxInputFile.Text);
}
回答10:
I had the same problem.
To sort out the problem.
Go to the custom class in Visual Studio.
Open the class and Check whether the constructor method.
If you have a constructor method, it should be parameterless.
If you have a constructor with parameters, don;t worry. Create another constructor class without an parameters.
Repeat this for all sub classes within the class.
Rebuild and run. Now your settings should save.