The question is simple: Can I see the stored values of my application configuration? Not at running time.
The only thing I see in app.config is this:
<setting name="myvariable" serializeAs="String">
<value />
</setting>
In my code I did a Save() but why I can't see the values in the file?
Settings.settings simply describes the design-time contents for user-specific settings. The actual runtime file which is created is user.config which can be found under the AppData folder under your specific Windows username and related to your application and the specific assembly that has the Settings.settings file within it.
If you want to check where it is at runtime, check the value of Environment.GetFolderPath(SpecialFolder.ApplicationData)
Related
I need to add configuration information to the assembly itself without including its app.config file with it.
How can i do it?
EDIT:
I need something like this
string config = #"<?xml version='1.0' encoding='utf-8'?>
<configuration>
.
.
.
.
</configuration>";
Set this hardcoded string configuration as current assembly configuration
Configuration settings be it user or application settings have their default values "hardcoded" in the assembly by default. They can be overriden by including an app.config, or modifying user settings at runtime and saving to the user config file.
After creating project settings (in project properties and go to the "Settings" tab), a Settings class is generated with static properties which will have the default value you configured.
They are accessible throughout your assembly like so:
Assert.AreEqual(Properties.Settings.MySetting, "MyDefaultValue");
These default values can be overridden via the app.config:
<applicationSettings>
<MyProject.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>MyDefaultValue</value>
</setting>
</MyProject.Properties.Settings>
</applicationSettings>
To answer your question: You can omit including the app.config from your application deployment, the defaults that you provided when configuring the settings are hardcoded.
Edit:
Just noticed you actually want to read the entire app.config from your assembly.
A possible approach could be the following:
// 1. Create a temporary file
string fileName = Path.GetTempFileName();
// 2. Write the contents of your app.config to that file
File.WriteAllText(fileName, Properties.Settings.Default.DefaultConfiguration);
// 3. Set the default configuration file for this application to that file
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", fileName);
// 4. Refresh the sections you wish to reload
ConfigurationManager.RefreshSection("AppSettings");
ConfigurationManager.RefreshSection("connectionStrings");
// ...
you can have a custom XML file where you store your settings and then you set it as Embedded resource so it will be available inside the exe or dll.
see here: How can I retrieve an embedded xml resource? for an example on how to read it at runtime
Edit: and to load it as custom configuration file check here: Loading custom configuration files
Currently I have the the default Settings.settings file that I access through Properties.Settings.Default.
But this config is saved in my user's appdata folder. How can I get there to be only one config file kept in the same dir as the exe that is universal AND can be changed at runtime?
You can't do this with the Properties.Settings feature, as the documenation says:
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.
An alternate approach would be to use the System.IO API to read and write a configuration file you design yourself.
You're gonna have problems with UAC on Vista/7.
Please, do keep vital application data files such as the "Settings" in a hidden and accessible directory - AppData.
UAC allows access to this directory and it's subdirectories.
If you want to share the file among all the users, you might want to use the "CommonAppData" directory, but you need administrator rights to write there.
You can place the settings in the application configuration file in the <applicationSettings> tag.
See http://msdn.microsoft.com/en-us/library/ms229207.aspx for documentation on the tag.
Eg (xxxxx -> the Visual Studio project name, yyyyy-> actual setting in the Settings file).
<applicationSettings>
<xxxxxx.Properties.Settings>
<setting name="yyyyyy"
serializeAs="String">
<value>some_value</value>
</setting>
</ReprintProcess.Properties.Settings>
</applicationSettings>
You cannot achieve it without extending and heavily modifying Properties.Settings class.
Therefore I would recommend either writing your own Settings Management wrapper or alternative libraries such as Settings4Net
My guess is that this question will fall under the category of "duh", but, nevertheless, I'm confused.
When using config files in, for example, a Windows Forms Application, the config file can be found in C:\Program files\CompanyName\ProductName\Application.exe.config. However, with the class library I'm developing I do not see a "ClassLibrary.dll.config" file in the install folder after installing it (in tandem with another project) from Visual Studio. Even though I do not see the file anywhere, retrieving data from it works correctly. Plus, running the following code from a method within the class library returns the path you would expect: C:\Program files\CompanyName\ProductName\ClassLibrary.dll.config.
If someone could shed some light on what I'm missing here, that would be really awesome.
public static string MyMethod()
{
Assembly assem = Assembly.GetExecutingAssembly();
Configuration config = ConfigurationManager.OpenExeConfiguration(assem.Location);
return "The assembly location was: " + assem.Location + Environment.NewLine +
"The config file path was: " + config.FilePath;
// Gives me "C:\Program files\CompanyName\ProductName\ClassLibrary.dll.config"
}
In a class library, the app.config file is pretty useless. Application setting values get stored in the Settings.settings file and are compiled into the Settings.designer.cs file.
If you modify the value of one of your application settings in app.config directly, that won't have an effect on the setting value seen while running the application. You have to actually open the Settings editor, at which point it will notice the difference between the app.config file and the Settings.settings file and inquire as to whether you'd like it to update your Settings.settings file using values from app.config.
Depends on the version of .NET. Prior to .NET 4.0, class libraries cannot use their own app.config files unless you do custom stuff to read the data in. In these scenarios, you should have the initialization of your class libraries require all of the appropriate data and pass that in from your consuming class's configuration (i.e. ultimately your winform's app.config file).
Your class library will use the config file of the executable that called it.
Your default value is stored in the dll.
But let's say you change the setting in code and save it. Where does it get stored for next time?
In WinXP look in:
C:\Documents and Settings\username\Local Settings\Application Data\
or in Windows Vista/7 look in:
C:\Users\username\AppData\Local\
You will find a folder named after your application and drilling down in to that folder, you will find a file named user.config.
For example:
C:\Documents and Settings\username\Local Settings\Application Data\MyApp\myapp.exe_urlla1ii3sytrhx0adqtjnjuc24oacqpgu4\1.0.0.0\user.config
Just to expand on #TLiebe's answer here, it is stored in the main assembly's settings file under a separate domain. For example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<TestLib.Properties.Settings>
<setting name="TestSetting" serializeAs="String">
<value>2.8.0.0</value>
</setting>
</TestLib.Properties.Settings>
<UpdateTest.Properties.Settings>
<setting name="Token" serializeAs="String">
<value>{fV234hwolihj453lvB}</value>
</setting>
</UpdateTest.Properties.Settings>
</userSettings>
</configuration>
I thought I knew this, but today I'm being proven wrong - again.
Running VS2008, .NET 3.5 and C#. I added the User settings to the Properties Settings tab with default values, then read them in using this code:
myTextBox.Text = Properties.Settings.Default.MyStringProperty;
Then, after the user edits the value in the options dialog I save it like this:
Properties.Settings.Default.MyStringProperty = myTextBox.Text;
Properties.Settings.Default.Save();
My question is, where is this new value saved? the MyApp.exe.config file in the executable directory is not updated, it still contains the default values. Plus, as far as I can tell, none of the other files in that directory are updated either! However, when the program reads the value back in, it gets the changed value, so I know it's saved somewhere...
This isn't just academic, I needed to be able to manually edit the value this morning and got myself stumped when I couldn't find anything that was changing.
In order to work with newer versions of Windows' policy of only allowing read access by default to the Program Files folder (unless you prompt for elevation with UAC, but that's another topic...), your application will have a settings folder under %userprofile%\appdata\local or %userprofile%\Local Settings\Application Data depending on which version of Windows you're running, for settings that are user specific. If you store settings for all users, then they'll be in the corresponding folder under C:\users or C:\Documents and Settings for all user profiles (ex: C:\users\public\appdata\local).
You can get the path programmatically:
using System.Configuration; // Add a reference to System.Configuration.dll
...
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
thanks for pointing me in the right direction. I found user.config located at this monstrosity:
c:\users\USER\AppData\Local\COMPANY\APPLICATION.exe_Url_LOOKSLIKESOMEKINDOFHASH\VERSION\user.config.
I had to uprev the version on my application and all the settings seemed to have vanished. application created a new folder with the new version and used the default settings. took forever to find where the file was stored, but then it was a simple copy and paste to get the settings to the new version.
it is saved in your Documents and Settings\%user%\Local Settings\Application Data......etc search for a file called user.config there
the location may change however.
if you use Windows 10, this is the directory:
C:\Users<UserName>\AppData\Local\
+
<ProjectName.exe_Url_somedata>\1.0.0.0<filename.config>
One of my windows services is logged on as Local System in windows server 2016, and I can find the user.config under C:\Windows\SysWOW64\config\systemprofile\AppData\Local\{your application name}.
I think the easiest way is searching your application name on C drive and then check where is the user.config
They are saved in YOUR_APP.exe.config, the file is saved in the same folder with YOUR_APP.exe file, <userSettings> section:
<userSettings>
<ShowGitlabIssues.Properties.Settings>
<setting name="SavedUserName" serializeAs="String">
<value />
</setting>
<setting name="SavedPassword" serializeAs="String">
<value />
</setting>
<setting name="CheckSave" serializeAs="String">
<value>False</value>
</setting>
</ShowGitlabIssues.Properties.Settings>
</userSettings>
here is cs code:
public void LoadInfoLogin()
{
if (Properties.Settings.Default.CheckSave)// chkRemember.Checked)
{
txtUsername.Text = Properties.Settings.Default.SaveUserName;
txtPassword.Text = Properties.Settings.Default.SavePassword;
chkRemember.Checked = true;
}
...
User-specific settings are saved in the user's Application Data folder for that application. Look for a user.config file.
I don't know what you expected, since users often don't even have write access to the executable directory in the first place.
For anyone wondering where the settings for apps from the Microsoft Store are, they are either in WindowsApps, which is very locked down, but you can get there by opening your app and then opening the file path with Task-Manager.
But it's more likely that they are saved in C:\Users\[USERNAME]\AppData\Local\Packages\[NUMBERS][COMPANY].[APPLICATION]_[RANDOMDATA]\LocalCache\Local\[COMPANY]\[APPLICATION].exe_Url_[RANDOMDATA]\[VERSION]\user.config.
There is a folder called "Properties" under your project root folder, and there are *.settings file under that folder. That's where it gets stored.
We have an "engine" that loads dlls dynamically (whatever is located in a certain directory) and calls Workflow classes from them by way of reflection.
We now have some new Workflows that require access to a database, so I figured that I would put a config file in the dll directory.
But for some reason my Workflows just don't see the config file.
<configuration>
<appSettings>
<add key="ConnectString" value="Data Source=officeserver;Database=mydatabase;User ID=officeuser;Password=officeuser;" />
</appSettings>
</configuration>
Given the above config file, the following code prints an empty string:
Console.WriteLine(ConfigurationManager.AppSettings["ConnectString"]);
I think what I want is to just specify a config filename, but I'm having problems here. I'm just not getting results.
Anyone have any pointers?
If your code sample for reading the AppSettings is in your DLL, then it will attempt to read the config file for the application and not the config file for the DLL. This is because you're using Reflection to execute the code.
Funny, where I'm at we're doing something very similar and the config file loads just fine. In our case I think each new config file's name matches that of it's associated assembly. So MyLibrary.dll would have a file named MyLibrary.dll.config with information for that file assembly. Also, the example I have handy is using VB.Net rather than C# (we have some of each) and all the settings in there are for the VB-specific My.Settings namespace, so we don't use the ConfigurationManager class directly to read them.
The settings themselves look like this:
<applicationSettings>
<MyLibrary.My.MySettings>
<setting name="SomeSetting" serializeAs="String">
<value>12345</value>
</setting>
</MyLibrary.My.MySettings>
</applicationSettings>
I wrote this for a similar system. My recollection is that I used Assembly.GetExecutingAssembly to get the file path to the DLL, appended .config to that name, loaded it as an XmlDocument, navigated to the <appSettings> node and passed that to a NameValueSectionHandler's Create method.
Here is one way -
AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file");
Call in constructor.
If I recall correctly, the app.config will be loaded from your application directory, so if you are loading dlls from some other directory, you'll want the keys they need in your application's config file.
I'm not totally sure but I think that class only works with the path of the entry method of the AppDomain (the path of the exe most of the time) by default.
You need to call OpenExeConfiguration(string exePath) (Framework 2.0 and later) first to point to a different config file.