User settings in C# - c#

I am trying to save Name Value pair in settings dynamically. I can see the new saved values dynamically. But, when I access the settings page from the property window , it shows the same old value. Here is the code :
Properties.Settings.Default.Name = textBox1.Text;
Properties.Settings.Default.Age = textBox2.Text;
Properties.Settings.Default.Save();
Any Suggestions?

Assuming you are testing your application with Visual Studio, your problem happens because, when you are changing the setting of your application you aren't changing the original setting file. When Visual Studio starts running an application, it creates a folder inside the directory where your code is named "obj/Debug" or "obj/Release" and copies all your DLLs and resources into that folders, including settings files.
This means that changes to settings will be reflected in your "obj/Debug/yourappname.exe.config" and not in the original file. If you open this file, for example, with a text editor you'll see the contents have changed. Remember that every time you recompile your application in Visual Studio and start running this file will be replaced by the original, losing your new settings.
You can manually run your .exe application inside that folder and validate if your settings have persisted.

After compile the settings file gets deployed into a yourapplication.exe.config file. This is the file you are changing (/Debug/app.exe.config). If you want to see changes in your "property window" you have to manually open the .settings file and edit the xml.
Note: after changing the .config file the changes are persistent .. but only until you compile your application again.

The solution->Properties->Settings is where you set the default values. User settings can be changed at run-time, and will persist with your application, but they are not written back to your visual studio project.
In your example, if you run the program again on subsequent times (* not by hitting debug / rebuild in visual studio), your settings that are saved in your snippet will have been persisted.
Imagine that your program has been deployed on user's pc's - there shouldn't be a mechanism for that to change your visual studio project settings file.

Related

Visual Studio user Properties.Settings.Default file from multiple projects?

So, despite a lot of info out there being difficult to understand and find on the latest Visual Studio settings file, I got that figured out. On your executable project, right-click and select properties, then left-click on the settings tab on the left, create a settings file (if needed), and fill in some settings names, types, and values in the table. If you want these to be permanent defaults that are embedded in the executable and read-only, set the scope to "application", but if you leave it at the default scope of "user", then you can save new values to a special user.config file in the system AppData folder which will be read from every time your app is launched, overriding the embedded defaults. This basically allows every user to have a config file automatically stored by the system saving some config settings, such as last window location, etc.
After setting up this, you can directly access the settings as follows:
var variable1 = Properties.Settings.Default.Variable1;
var variable2 = Properties.Settings.Default.Variable2;
and so forth. To set the properties to a new value is equally easy:
Properties.Settings.Default.Variable1 = variable1;
Properties.Settings.Default.Variable2 = variable2;
And then, if you want to save the special user file with the current values into something like C:\users\username\AppData\Local\ApplicationName.exe.xxxx\user.config, then just use:
Properties.Settings.Default.Save();
Here is the issue - I'd like to access those same settings from all files in my solution, but the auto-generated methods are marked internal, so they can only be accessed from my main exe project. I can create a class to essentially export the settings, but this only seems to work if the startup executable is always the same. When I run unit tests (with xunit 2.0), I find that the settings are not set - if I use the library to set them and save, they show up in a Microsoft folder in AppData, that appears to correspond with Visual Studio.
Does anyone know a way I can reliably access the Properties.Settings.Default values of a project across an entire solution, or am I stuck going with a straight file at a known location and all the I/O and parsing that entails?
If so, is there a standard C# config file library that is used for this task outside of the built-in Visual Studio settings system?
Edit - Update:
The settings file appears to be based on the following:
The Company Name in the Assembly Information (Properties/AssemblyInfo.cs or Project Properties/Application/Assembly Information/Company)
If the Company is empty, it appears to use the Default namespace from (/Application/Default namespace), which matches the namespace used in Properties/Settings.settings/Settings.Designer.cs
The Executable Name (i.e. app.exe)
This is strictly the executable name at runtime, including extension, so if you rename the file, that will change the settings file name and location in AppData
The Assembly version
The folder within AppData/Local will be #1 above and then a nested folder will have the executable name (#2 above) with some other characters appended, and then a final nested folder will be the version (#3 above), and the file inside that will be user.config.
So, what appears to be happening is that when the unit test runner executes, the company of the runner is used for #1 (the built-in VS runners will use "Microsoft", and the Resharper runner will use "JetBrains", etc.), the exe name will still start off with the Namespace of the tests, and then the version number is also from the test runner (I think).
This means when running unit tests it will always look for and save to a different settings file than the main executable - so when you access the Properties.Settings.Default parameters from any assembly, it will look for the file in a different location (and save it to a different place) when running unit tests than from running the main executable.
You can always expose objects marked as internal by adding the following to the AssemblyInfo.cs [assembly: InternalsVisibleTo("UnitTestProject1")] in the main exe project. You should then have access to the internal objects from the test project.
https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

How to open app.config in windows forms?

I need to open app.config with a click of a button. I've been trying to find how to do this with no luck. How can I open the file itself so I could edit it?
If you need to open the application configuration file itself you're doing something wrong.
If you need to access settings defined using Visual Studio's settings designer, use the automatic properties like
var value = Properties.Settings.Default.SettingName;
If you want to edit user settings, do the same thing and save:
Properties.Settings.Default.SettingName = Value;
Properties.Settings.Default.Save();
You can not modify application settings anyway. If the application is installed in Program Files you will not even be allowed to write the file back, as the Program Files folder is not writable to your application.
If the application is installed using ClickOnce you're also out of luck, as the settings file for user settings is named totally differently and is not even in the same location as your application's executable.
So this comes down to: Do it right and keep your hands off app.config!

Application does not pick updated values after modifying App.Config file c#

I have developed a winforms application which consists of a file path value (fetching crystal reports) in add key of app.config file.
I have installed the application and trying to change the file path (in app.config as well as exename.exe file) from old to new but the application doesnt take new crystal reports file path instead retrieves old data.
Am i missing any other file to be updated ? Kindly help to get it solved ASAP.Thanks
You probably don't want to change App.config, as that file will be copied to exename.exe.config when you build.
After installing your program, you'll need to change your exename.exe.config file to pick up your changes.
Verify that the setting you are trying to change in exename.exe.config file is set to User scope rather than Application.
From Visual Studio go to your Project properties and Choose Settings
You will find listed all your settings. From there verify that each setting you want to be change by user has scope User
Here you can find detailed information on the difference between User and Application scope

Change to Application Setting is not showing up at runtime

I have an application that's dealing with a required list of files it has to manipulate when run. I wanted to add a couple of files to this list. The list of files is stored in a setting. The scope of the setting is "Application". I added my two files to the list (via project properties -> settings), and saved everything. My files now appear there. My files are showing up in the app.config for this project under ApplicationSettings.
But when I run the application this line of code:
StringCollection requiredFiles = mySettings.UpdatePackageRequiredFiles;
Does not include my files.
mySettings is the right type; it's the project where the settings exist. Further, there is nowhere in the project UpdatePackageRequiredFiles is being written to and you can't write to an Application scope setting at run-time anyway (or so I understand).
To spice up the mystery a little bit, there are 3 files appearing in the list whose existences are a complete mystery. They do not appear when viewing the settings in the designer, or in the source file. So what am I missing?
It turns out there is a configuration file with an identically named setting that the application was getting the list of files from.

C# project installer - where are user settings saved in an installation context?

I'm trying to set some user configurations in an installer. For instance, I'm using:
Properties.Settings.Default.mapURL = txtBoxMapURL.Text.Trim();
Properties.Settings.Default.Save();
in a Windows Form that the installer class calls. However, upon launching the application, the setting doesn't persist. The next time I try to configure the setting in the installer, it reads the correct value into the textbox. So it's saving the setting somewhere, I'm just having a hard time figuring out where.
It's not being saved in C:\program files\[manufacturer]\[product]\[product].exe.config, and also not in C:\Documents and Settings\[User]\Local Settings\Application Data\[Manufacturer][Product].exe\user.config.
Any idea where the installer is temporarily storing the setting, and is there a way to store a user setting during an installation?
The place they get saved to is the user.config... check: http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
Is the scope of your settings 'user' and not 'application' - as the application guys can't be saved.
Do check THIS out: How can I set application settings at install time (via installer class) ... seems like a similar issue.
The user.config file was being updated from the installer, but it is saving and reading the config (for my particular application) from:
C:\Documents and Settings\Long\Local Settings\Application Data\Microsoft_Corporation\DefaultDomain_Path_w551cnaciyzcylzfdpgyceaw05mmrhk0\3.1.4001.5512\user.config.
Unless there's another way to update the correct user.config file, it looks like I'll have to run a runonce type of thing when the application is first launched.

Categories

Resources