I have an application where I need to store 3 set of string collections (user bookmarks and some other stuff). I have added a "Setting File" to my C# project, created the 3 settings variables of type Specialized String Collection. Everything appears to be fine, as I can add strings to each of the collections and invoke the .Save() method of Settings.Default. I can see my pretty string collections within the generated "user.config" file.
HOWEVER, when do these values get loaded? There is no .Load() method of Settings. Attempting to access the Settings.-setting variable- name throws a null exception error, so obviously these values are not loaded at launch time on their own.
What am I missing?
Perhaps you're having problems with differing versions of the application and settings file? Try calling ApplicationSettingsBase.Upgrade to migrate previous settings to a new version.
Note that you should only be calling Upgrade once, and only when you've actually updated your settings. Create a settings property called NeedsUpgrade with a default value of true, and execute the following when you application loads:
if (Settings.Default.NeedsUpgrade)
{
Settings.Default.Upgrade();
Settings.Default.NeedsUpgrade = false;
Settings.Default.Save();
}
With this, Upgrade will only be called when a new version of a settings file is created.
Related
Good evening,
I happen to have an issue with Winforms and Resources.resx.
The app I'm working on is built via 'Winforms App' template selectable via Visual Studio.
The issue I'm having is kind of outside of winforms localization - which I think is important to mention.
Here's my issue:
- I've created a 'HistoryManager' class, which has only one method and one action to perform - add a history to the SQL database.
- I've created Resource.resx file, entered a format string into it under "MsgTaskAdded" which is equal to "Task {0} has been added to {1}".
- The said string adds to the database flawlessly.
Resources.resx works well - if I change MsgTaskAdded resource string, it changes what will be added to database .
Now, the issue I'm having is.
- I've created a Resources.de-DE.resx file, copied strings from Resources.resx and translated to german.
- Changed CurrentThread.CurrentCulture and CurrentThread.CurrentUICulture to 'de-DE'.
As a result, the text added to database is STILL in english, as if the file wasn't found and it fell back into using the default Resources.resx.
Make certain that your Resources.de-DE.resx file is contained within the Properties folder of your project, and that it has a Build Action of Embedded Resource, so that it gets properly associated to your default Resources.resx during compile. The culture settings on CurrentThread also need to happen before the call to the database, ideally somewhere in your static void Main() function before the Application.Run(…) call.
This should produce a culture-specific folder in your bin\<build_configuration> folder that contains a file named <your_app>.resources.dll. If needed, you can crack this open with a tool like ILSpy or Reflector to verify that the translated resources exist in the expected place within the assembly.
Maybe the reason in that how you're changing the thread culture. I did exectly what you did and its worked. That is how I've changed the culture
static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
Console.WriteLine(Resources.Hello);
}
it prints "Hallo"
I found a solution.
If you guys ever have issue with the Resources.resx, make sure the localized file isn't: Resources.de-DE.resx or Resources.pl-PL.resx, instead - use only the first bit (Resources.de.resx) and make sure to open the file and set accessors to 'Internal' so it generates you a Designer file with proper code.
I'm using Settings.Settings to store settings at run-time. It was very helpful for my earlier application to store data. But in my current project its not saving the settings data. My application have some tab and each tab contains some TextBox. Im using textBox Text to store string values.
Properties.Settings.Default.Setting1 = textBox2.Text;
Properties.Settings.Default.Save();
It is a working method for my all previous application.But I can't understand why its not working in my current project.
Since your question does not include many details, I've tried to answer as much as possible:
Try to perform a Properties.Settings.Default.Upgrade() and then saved settings get loaded.
You have to call the Upgrade method of ApplicationSettingsBase derived class (that is normally called Settings and is created for you by Visual Studio)
Properties.Settings.Default.Upgrade();
When/where to call the Upgrade method? There is a simple trick you can apply: define a user setting called UpgradeRequired (example) as bool (the easiest way is through IDE). Make sure its default value is true.
Insert this code snipped at the start of the application:
if (Properties.Settings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
}
So the Upgrade method will be called only after the version changes and only one time (since you disable further upgrades by setting UpgradeRequired = false until a version change - when the property regains default value of true).
Check the scope of your settings [USER/APPLICATION]
Try this out, and if it doesn't work check the below conditions.
Also, a more detailed question next time would be much appreciated.
Permission (NTFS permission)
Or Active directory permission
Or capacity of windows drive is full.
Or there exist two or more user folders and you checked another.
For example: There are two or three folders, user.domain, user.workgroupname, user.
Despite all this, I suggest you to learn about System.Reflection and develop your personalized method to save settings, the option provided by the Visual Studio isn't very dependable.
Hope this was helpful.
I'm developing an application that saves various user settings (e.g. window locations, options, preferences). This is done using the syntax
Properties.Settings.Default.setting_name = "xxx";
Properties.Settings.Default.Save();
and
var x = Properties.Settings.Default.setting_name;
Here is an example of the underlying settings file path:-
C:\Users\user_name\AppData\Local\company_name\exe_name.vsh_Url_kxrjspzszls01bmlnkpeuf5cutfdioia\1.0.0.0
The problem is that each time I build and release a new version of the software, users are losing their settings. This is presumably down to the fact that the application exe version number is being included in the file path, so each time a new version is installed it starts over with a new, empty settings folder? Fortunately we're still in the dev phase so it's only a couple of internal users at present.
What's going on, and is there a way around it?
I'm also concerned because we have an existing product out in the field, and are due to release a new version shortly. I'm panicking that all these users will lose their settings and preferences when they upgrade.
So based on other SO questions and articles found on the web, I've written this short method that I now call during application startup:
private static void CheckForUserSettingsUpgrade()
{
if (!Properties.Settings.Default.UserSettingsUpgradeRequired)
{
return;
}
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UserSettingsUpgradeRequired = false;
Properties.Settings.Default.Save();
}
For this to work there must be a user-scope setting called UserSettingsUpgradeRequired with a default value of True.
After an upgrade (resulting in a new settings file being created) this setting's value will of course be true. In this scenario the above code will import all settings from the previous settings file (if any), set this upgrade flag setting to false, then save the changes. After the next upgrade, the flag's value will revert to true, and the process will repeat itself.
I was just wondering if anyone has any input on how to save a C# Winform setting?
Currently, I have a form that has various radio buttons, directory browsers, date pickers etc. I was wondering what is the best strategy to save these settings to an external file that can be loaded at a later date. So essentially each configuration can be loaded, executed, and then another configuration loaded. Also, the configuration can be passed across installations / users.
Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.
Write and Persist User Settings at Run Time
Access the user setting and assign it a new value, as shown in the following example:
Properties.Settings.Default.myColor = Color.UserGreen;
If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.Save();
I solved this problem with a class or struct which contains all settings. My form-class had a constructor which accepted such a setting-instance.
This settings-class/-struct was implementing ISerializable. So you can save it easily into files and load it from.
This is by far not the best way to do it, but it is quiet easy to implement.
I'm using C# .NET 2.0 Windows Application.
and I'm using app.config for my Application Settings.
but change in AppSettings doesn't reflected runtime, it Needs Application to be restarted.
How can I avoid it.
Here is my code snippet I used to read and write the Application Settings.
I'm reading the Setting like this
string temp = ConfigurationManager.AppSettings.Get(key);
I'm updating the value like this where node is the current configuration/appSettings Node
node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
You could try calling ConfigurationManager.RefreshSection("appSettings") to refresh the AppSettings section of the file from disk. Once they have been refreshed, you should be able to read the new values.
I've just tested this and it does indeed work.
Alternatively, you could create a singleton 'Options' to hold on to your application settings and perform your read/writes for you. Once loaded, changing the .config doesn't require reloading, you simply set a property on the singleton and call your .Save() method.
The 'runtime' version of your settings is in the singleton, no need to read from disk.
Dont use ConfigurationManager to read settings, instead use:
System.Configuration.ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).AppSettings.Settings["value"];
ConfigurationManager.RefreshSection("appSettings");
works!!
But be careful that if we are in debug mode, the configuration file can be called xxxxx.vshost.exe.config, where xxxxx is your project name.