Get path to config file which is stored in AppData - c#

I have to read app setting file and get some value from another my application. My settings class is located in a separated asembly, but when I try to get value:
var id = MyAppSettings.Default.UserId
I get default value which is equal 0. I understood that setting file is 'exe' specific. Setting file is stored in
%USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<version>\user.config
I tried to get path and have found this SO answer. But this code also return 0(my default value), because it looks config file in the local folder.
How to properly read setting file (not app.config and local)?

You can use this, if user.config file is accessible to your user and you have permission to read this file ::
string path=#"%USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<version>\user.config"
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(path);
//and then access setting and so on....

Related

write to a different programs app.config c#

I am needing to find away to write to another programs app.config file.
we have a 3rd party application that gets some values from an AppSettings section in its own config. but the only way we can get to change the value is when we call the application and once it runs it does a function which we don't want it to do if it doesn't have the correct values.
We need to encrypt one of the values so what we were think of was creating an application that would save the values in this 3rd party app.config.
saving the values and the encryption aren't a problem its doing it in another applications config file.
Is there a way to set which config file we use or a config path setting?
Regards
Aidan
Thank you for the help with this, I managed to get it done through this code.
string configLocation = string.Format("{0}\\APP.exe.config", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = configLocation;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
txtEndpoint.Text = config.AppSettings.Settings["ENDPOINT"].Value;
This now allows me to write out the set config file values in the appsettings.
Cheers All.
Aidan

Set value in app.config - no write access to Program Files

I would like to change values in my app.config file (or rather MyProgramName.config file) at runtime. This works fine using
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//update values in config.AppSettings...
config.Save(ConfigurationSaveMode.Modified);
However the MyProgramName.config file is always stored in the program folder according to the documentation (https://msdn.microsoft.com/de-de/library/system.configuration.configurationuserlevel%28v=vs.110%29.aspx)
Application configuration files are in the same directory as the application and have the same name, but with a .config extension. For example, the configuration file for C:\System\Public.exe is C:\System\Public.exe.config.
Now lets assume I have some users who can not write to C:\Program Files(x86)\... (which is the default setting for non-admin users on win7). Is there a way to still use the app.config file properly or maybe place it in the %APPDATA% folder?
You can use the User settings capability of the application settings api for this. These settings are stored in %userprofile%\appdata\local or %userprofile%\Local Settings\Application Data (windows version dependent).
User settings has some limitations: it is, as it says, user specific not global for all users - but presumably if you are updating values at runtime then that's what you want, otherwise you need something like this.
You essentially just have to add a .settings file, which creates the applicationSettings and/or userSettings sections in your app.config (see MSDN: How To: Create a New Setting at Design Time), create your property and set it to User, not Application, and then do this at runtime:
Properties.Settings.Default.myColor = Color.AliceBlue;
Properties.Settings.Default.Save();
The .settings property will create a user settings entry in your app.config that looks like this:
<setting name="Setting1" serializeAs="String" >
<value>My Setting Value</value>
</setting>
You can use this to set the default value that a user session will get before any user-specific value has been saved.
Reference: MSDN: Using Application Settings and User Settings
Data, stored in app.config, are not intended for changing by regular user at run-time. Do not write a code, which re-writes app.config, except the case, when this code is located at some config application for admin. Use application settings instead.

App.Config not updating

I want to update app. config file in windows form c# . here is a code for updating app. config
// updating
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("name");
config.AppSettings.Settings.Add("name",txtName.Text);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
// show updating result on labels ( and this work fine )
string value = ConfigurationManager.AppSettings["name"];
lblName.Text = value;
this update work fine when I am running an application but when I restart application all config is reset to default
Instead of using the Configuration class, open the file as a regular Xml file and make your changes. Note that when you are doing this, when you save the file back, any Comments in your .config file will be removed by the Xml class. To prevent this, you must read ALL types of Xml nodes from the original configuration file and write them all back.

ASP.NET database connection string error of AttachDBFilename property

I'm trying to set the AttachDBFilename property of connection string in Web.config, with the absolute path of the mdf file, it works, I want to use |DataDirectory| instead of the absolute path, here is what I tried, but it does work.
AttachDbFilename=|DataDirectory|\Database.mdf;
As described here , try to check if there is a log file under the same directory with your mdf file, if there is, remove the log file, Once the database is attached, a new log file will be automatically generated based on the physical path.

Not able to persist new changes in appsetting section in app.config WPF?

Below is the code i am using to update or change the values in appsetting in app.config
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["userName"].Value = username;
config.AppSettings.Settings["pwd"].Value = pwd;
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("appSettings");
i am using above code to change or update the settings in appsetting section at runtime and want the changes to persist so that when i run the application it should pick the new values from appsettings but here it doesn't happen so the changes made and saved at run time do not persist when i relaunch my application again it has the old default settings. Also i checked app.config in bin/debug but it too had the old values in appsettings. i refered various blogs and post here too as a reference but it got the same code as above but it did not persist the settings.have referred this post
I had the same problem while ago. I would have preferred to put this in a comment but I don't have that privilege. My answer might not be your case but I think is worth to be shared.
May I ask you where your bin folder is located? Windows 7 when you programmatically alter a file that isn't in a user accessible space creates a copy of that file in a Roaming space and there the file will stay. Every time you try to access the file (like your app.config) W7 transparently redirect your readings/writings to this file, so there's a chance that you are modifying the file in the roaming space, leaving the one you are lookin unaltered.
Are the changes you are making still there the successive time you start the application?
Disclaimer/Apology: I'm not an experienced user so if I am saying silly things let me know and I will remove this comment.
See below(from MSDN) and remember app.config is in your project. .exe.config is the actual file name.
Client applications use a global configuration that applies to all users, separate configurations that apply to individual users, and configurations that apply to roaming users. The userLevel parameter determines the location of the configuration file being opened by indicating whether it has no user level (the configuration file is in the same directory as the application) or has a per-user level (the configuration file is in an application settings path determined by the user level).
Specify which configuration to get by passing one of the following values for userLevel:
To get the Configuration object that applies to all users, set userLevel to None.
To get the local Configuration object that applies to the current user, set userLevel to PerUserRoamingAndLocal.
To get the roaming Configuration object that applies to the current user, set userLevel to PerUserRoaming.
NoteNote
To get the Configuration object for a resource, your code must have read permissions on all the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write permissions for both the configuration file and the directory in which it exists.
i got my solution of above problem, my goal was to persist changes done at run time at application or user level. Initially i tried using App.config where i kept default settings for application in appsettings section of app.config, but later after research i got to refer i got to know appsetting does not persist the changes, instead you can use userSettings section where under YourApplication.Property.Settings you can give your userlevel settings and it worked for me. To do this you do not need to go to App.config to do it manually, rather you can do it from the property window of project.
Right Click on your project -> Select Settings Tab on the left-> Now on the right hand side you will see the Resource section , give the ResourceName, Type, Scope and its value and you are done. The same value can be access and change dynamically from Code as well.
Below are Code Excerpt for the same --
Accessing Settings Value
enter code here
userName = Properties.Settings.Default.UserName;
pwd = Properties.Settings.Default.PWD;
Saving New Settings Back
enter code here
Properties.Settings.Default.UserName = userName.ToString();
Properties.Settings.Default.PWD = newPWD..ToString();
Properties.Settings.Default.Save();
And when you will launch your application next time you will get the new changed settings as your default settings.
I hope that helps
Thanks Guys
VJ

Categories