C# Settings.Default.Save() not saving? [duplicate] - c#

This question already has answers here:
Why are my application settings not getting persisted?
(5 answers)
Closed 5 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!

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.

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 -

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.

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

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

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.

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.

did you try calling ConfigurationManager.RefreshSection before checking that it got saved ad you could try it again after the reload

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.

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.

Related

Visual Studio during Debugging: The function evaluation requires all threads to run

I'm suddenly getting a strange error while debugging. Up to now the variable in the watch windows has been showing correctly. Now I am always getting this error message in the watch windows:
The function evaluation requires all threads to run
I am not able to check any variable anymore. I am not explicitly working with threads. What can I do to get it working again?
I already disabled, as mentioned in some forums, the function: "Enable property Evaluation and other implicit function Calls" in the option window of the debugger. But without success, and it gives me this error:
Error Implicit Function evaluation disabled by the user
From the msdn forum:
This isn't an error in and of itself, but more of a feature of your debugger.
Some properties require code to be executed in order for the property to be read, but if this requires cross-thread interaction, then other threads may have to run as well. The debugger doesn't do this automatically, but certainly can, with your permission.
Just click the little evaluate icon and it will run your code and evaluate the property.
For further details on this behaviour check this excelent article
I ran into this issue when just trying to get items from a table called "AGENCY" using Entity Framework:
var agencies = db.AGENCY.OrderBy(e => e.FULLNAME);
Hovering over agencies in debug mode, clicking to expand the options, and clicking Results would give the dreaded "The function evaluation requires all threads to run" with a "Do Not Enter" icon at the end that, on which, clicking did nothing.
2 possible solutions:
Add .ToList() at the end:
var agencies = db.AGENCY_TABLE.OrderBy(e => e.FULLNAME).ToList();
List<AGENCY_TABLE> agencies = db.AGENCY_TABLE.OrderBy(e => e.FULLNAME).ToList();
Credit goes to Hp93 for helping me come to this solution. In the comments on MUG4N's answer where I found this solution, it also mentions trying .Any() instead of .ToList(), but this gives a Boolean instead of a <T>, like <AGENCY> is, so it probably wouldn't help.
Workaround - try a different path in the debug options. I found that I could click on the "Non-Public Members" > "_internalQuery" > ObjectQuery > Results View and get my values that way.
MUG4N has indeed provided a correct answer however if you hover over the line of code in debug, you may be looking at something like the below. If so, click the little re-evaluate icon highlighted in the image below...
NB: I obtained this image by pinning, normally the re-evaluate icone are in the middle of the window and not down the left hand column.
You should make thread safe call because accessing Windows form controls are not Thread safe in multithreading.
This is my simple code which makes Thread safe call and sets Progress bar.
public partial class Form1 : Form
{// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void StringArgReturningVoidDelegate(string text);
private Thread demoThread = null;
public int Progresscount = 0;
static EventWaitHandle waithandler = new AutoResetEvent(false);
public Form1()
{
InitializeComponent();
}
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
{
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
}
catch
{
return false;
}
}
public void Progressincrement()
{
waithandler.WaitOne();
while (CheckForInternetConnection()==true)
{
if (Progresscount==100)
{
break;
}
SetLabel("Connected");
Progresscount += 1;
SetProgress(Progresscount.ToString());
Thread.Sleep(TimeSpan.FromSeconds(1));
}
if (Progresscount <100)
{
Startthread();
}
SetLabel("Completed");
}
public void Startthread ()
{
this.demoThread= new Thread(new ThreadStart(Progressincrement));
this.demoThread.Start();
SetLabel("Waiting for connection");
while (CheckForInternetConnection() == false) ;
waithandler.Set();
}
private void SetLabel(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.label1.InvokeRequired)
{
StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetLabel);
this.Invoke(d, new object[] { text });
}
else
{
this.label1.Text = text;
}
}
private void SetProgress(string Value)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.progressBar1.InvokeRequired)
{
StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetProgress);
this.Invoke(d, new object[] {Value});
}
else
{
this.progressBar1.Value = Convert.ToInt32(Value);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Startthread();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Responsive");
}
}
For more information MSDN
This isn't an error, but more of a feature of your debugger.
The debugger doesn't do this automatically, but certainly can, with users permission. Just click the little space icon and it will run the code and evaluate the property.
I use the next workaround to pass:
var OtherThreadField = "";
Invoke(new MethodInvoker(delegate
{
OtherThreadField = ExecuteNeededMEthod();
}));
Now i have a value for OtherThreadField.
I faced the same issue and solved .The Issue arise due to username and password ,in SQL connection there is user and password but in code there no user and password. so I enable the user and the password and the issue solved
For me, this happened when trying to break on a line that accesses a complex object instance contained by a Settings Class.
A breakpoint on the following if results in Settings.Default.FindSettings with the value being "The function evaluation requires all threads to run." If I press the force eval button, it is null. Stepping with the force eval button click or not enters the if block and initializes the object. If I remove the breakpoint and add a new breakpoint following the if block, the Settings.Default.FindSettings deserializes properly with the expected values.
if (Settings.Default.FindSettings == null)
{
Settings.Default.FindSettings = new FindSettings();
}
After trial and error, I added the following code before the above if block to access the settings prior to breaking. This seems to reliably fix the problem. I do not need it in production so I wrap in conditional compiler directive. I have a comment in the code instead of a non-descript discard:
#if DEBUG
var _ = Settings.Default.FindSettings;
#endif
I am not sure if the above line would be optimized out in production since it has side effects. As I only need it while debugging, I have not checked.

Get and Set not being called

So I have a couple fields in a program that set the email subject and email body for an automated message.
My Email Settings class is this:
public static class Email
{
public static string Body
{
get { return (string) SettingsStore.RetrieveSettingValue("emailBody"); }
set { SettingsStore.StoreSetting(new Setting {SettingKey = "emailBody",
SettingValue = value}); }
}
public static string Subject
{
get { return (string) SettingsStore.RetrieveSettingValue("emailSubject"); }
set { SettingsStore.StoreSetting(new Setting {SettingKey = "emailSubject",
SettingValue = value}); }
}
}
And my UI code that gets and sets the values is here:
private void ApplicationSettings_Load(object sender, EventArgs e)
{
subjectTextEdit.Text = Settings.Email.Subject;
bodyTextEdit.Text = Settings.Email.Body;
}
private void ApplicationSettings_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Email.Subject = subjectTextEdit.Text;
Settings.Email.Body = bodyTextEdit.Text;
}
Oddly, my get and set methods in the Email Settings class are not being called- when I access this form for the first time after application start the fields are blank even when the values are in the database. After I edit the fields once and close that form then open the form again (without closing the entire application) the fields have the text I put into them.
I have set a breakpoint on the _Load and _FormClosing events and these are getting hit.
When I set a breakpoint on the get/set methods inside the settings class, the breakpoints are not getting hit.
Any ideas?
To everyone: Thanks for your help, I found the issue- the issue was Visual Studio, not the code.
I closed VS, restarted the PC, opened VS, cleaned the solution, closed, reopened, built. There was an error in one of my referenced assemblies that was not appearing all the other times I built the project that appeared this time, and fixing that error caused all of this to work properly.
I'd have never found this without your help (I'd still be scratching my head wondering how my code was wrong) so thank you all so much! :)

C# Passing Arguments Through Context Menu

so I made a program that creates a registry key on the pc, that adds an option to push your file to your device. (The program itself pushes files to your Android sd card.)
Everything works well, or at least should, once I figure out this issue. When I right-click on an item and choose my command (which is to open up with my program) my program just opens up, but no arguments are passed.
A month ago, I designed a program that opens when the user double clicks (just double click, not right-clicking through context menu) a file, it opened the specific program, and from there I was able to grab the arguments.
Why can't I from the context menu?
Thanks, sorry for lengthy post.
And here's part of my code:
private void Form1_Load(object sender, EventArgs e)
{
string c = textBox3.Text;
string[] args = System.Environment.GetCommandLineArgs();
try
{
string location = args[1];
MessageBox.Show(location);
//For testing purposes only
Properties.Settings.Default.thinglocation = location;
Properties.Settings.Default.Save();
Process();
}
catch
{
}
Here's the registry code. BTw string c is "C:\File push.exe" the location of my program.
string MenuName = "*\\shell\\NewMenuOption";
string Command = "*\\shell\\NewMenuOption\\command";
RegistryKey regmenu = null;
RegistryKey regcmd = null;
try
{
regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
if (regmenu != null)
regmenu.SetValue("", "Push to Android");
regcmd = Registry.ClassesRoot.CreateSubKey(Command);
if (regcmd != null)
regcmd.SetValue("", c +"%1");
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
I assume that you have created a registry entry which creates the context menu item which you are using to launch your program. Likelihood is that you need to add ' %1' after the name of the exe you want to launch. In the context menu registry entries %1 represents the selected file, so adding this should launch your program and provide the name of the selected file as the single argument.
the reason why it worked before with the double click is that windows will launch the default program for the file double clicked, with the double clicked file as the argument.
Did you try looking in the sender or the EventArgs parameters? I would think their might be something in there. That's just a guess though. If its just a double click then why would there be any arguments? Maybe I am just misunderstanding that point. Anyways, maybe that helps. If not maybe you could clarify a little bit where the arguments are supposed to come from.
Erik

failed to update a static property

public static class clsCounter
{
static int count;
public static int Counter
{
get { return count; }
set { count = value; }
}
}
The above is the static class that is used to record a number.
Also, I have two projects within a VS2010 solution, one of which is a class library. In one of these classes, I have got the following code which uses clsCounter.
if (clsCounter.Counter == 0)
countIES++;
else
countIES = 0;
Now, in the other project, I set some new values to clsCounter
clsCounter.Counter = 50;
However, for some reason, I am not able to set clsCounter.Counter to 50, thus I always get countIES++. The code looks okay to me, and I have no idea what's wrong with it? Can anyone help?
Thanks.
EDIT:
I wonder if it has something to do with the scope of projects within vs solution?
Solution Structure
Solution
ExcelAddIn
Form1.cs => (clsCounter.Counter = 50)
...
ClassLibrary
clsCounter => (static class)
...
EDIT 2:
clsCounter.Counter = 50; is actually running in backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) event. Could this be a possible issue?
EDIT 3:
I have uploaded a sample project that seems to be able to reproduce the same problem. Here's the shared link: => http://www.4shared.com/folder/sInyNWyi/_online.html
What I would like to do here is to populate a cell with value, Other case after the button 'set value' is pressed. The static class and UDF can be found in the class library.
Note that, to be able to use =testFunc() within excel addin, need to find it in automation server list and enable it. So just go File->Option->Addin->Under Manage Add-in->Click GO->Automation->Ebale ClassLibrary1.UDF
Please also check if the option "Register for COM interop" has been enabled or not before launching the debugger. To find it, go ClassLibrary1 Property -> Build -> Under Output, check Register for COM interop.
Add the following line to your static property:
public static class clsCounter
{
private static int count;
public static int Counter
{
get {
Debug.WriteLine("Counter viewed");
return count;
}
set {
Debug.WriteLine("Counter Changed from {0} to {1}", count, value);
count = value;
}
}
}
Then you can watch your debugger and set breakpoints on the counter which will allow you to find out which part of the code is modifying your counter inappropriately. A static counter will be initialised "at some time" before it is accessed. I would say you are setting the counter and something somewhere is immediately incrementing it before you read it.
Finally, I think I found a workaround although it had nothing to do with the static. I was kinda inspired by the idea of using cookies in the web apps.
Similarly, all I need to do here is:
store a value in a temporary text file, by doing
System.IO.File.WriteAllText(#"C:\countIESValue.txt", value);
in the "set value" button click event handler.
And read the stored value whenever I need it from the above text file and assign it to a local variable.
if(System.IO.File.Exists(#"C:\countIESValue.txt"))
{
string val = System.IO.File.ReadAllText(#"C:\countIESValue.txt");
}
The text file can also be deleted after done processing. In this way, I don't have to worry about any scope or application domain issues, although the permission of writing files is required. I am glad it worked pretty all right for me.

Can you change a setting in the Foo.settings file at run-time?

I have the following code that determines if it's the first time the user opens the app. If it is, a file dialog shows, and then it's supposed to change the setting to false, so it doesn't happen after that one time.
public void VerifyIfFirstTimeRun()
{
if (Properties.Settings.Default.FirstTimeUse == true)
{
LocateWoWFolder();
Properties.Settings.Default.FirstTimeUse = false;
}
}
In the last line, I receive the following error:
Error 1 Property or indexer 'CDLauncher.Properties.Settings.FirstTimeUse' cannot be assigned to -- it is read only
How can I do this?
The scope of your setting is probably set to Application. Set it to User, you will be able to modify it.
I found out you can indeed change the .settings at run-time, but only setting in the "User" scope.
So if you're trying to make a setting that does X only when the first time the app opens, you can do the following:
public void VerifyIfFirstTimeRun()
{
if (Properties.Settings.Default.FirstTimeUse == true)
{
//Do something here.
//Change first time usage Bool to false
Properties.Settings.Default.FirstTimeUse = false;
//Save your changes, and you're done!
Properties.Settings.Default.Save();
}
}

Categories

Resources