Write Values in App.Config File

Write values in app.config file

If you are using App.Config to store values in <add Key="" Value="" /> or CustomSections section use ConfigurationManager class, else use XMLDocument class.

For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="server" value="192.168.0.1\xxx"/>
<add key="database" value="DataXXX"/>
<add key="username" value="userX"/>
<add key="password" value="passX"/>
</appSettings>
</configuration>

You could use the code posted on CodeProject

How to write value to app.config file permanently in a console application?

As far as i know information from app.config is used while creating program but app.config keys and values are read-only, but you don't have to use app.config to store values in app, try using this:

step 1
go to solution explorer -> Properties -> Settings and create settings file.

step 2
add settings you want to use

step 3
Use in code

add using yourNameSpace.Properties;

read value of setting:

var name = Settings.Default.Name1;

seting value of setting:

Settings.Default.Name1 = value;

saving settings:

Settings.Default.Save();

Hope it helped you.

Write a section in app.config file

Putting that information in the config-file is only one step to achieve what you're looking for.

Your <Example>-node is a custom section, that's unknown at that time. For enabling the ConfigurationManager to parse your section to an actual object at runtime, you'll have to define your section as a class deriving from ConfigurationSection:

public class ExampleSection : ConfigurationSection
{
[ConfigurationProperty("file", IsRequired = true)]
public string File
{
get
{
return this["file"];
}
set
{
this["file"] = value;
}
}

For a complete example, please have a look at this comprehensive MSDN-article.

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

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

How to display the values from an app.config file

As you need the value of the key so you you should do something like following inside your loop.

And you doesn't need the foreach loop.

Just use as following it will help you

As you are using the foreach loop, which is not required at all.

So remove it as because it will increase the time-complexity.

In case you have multiple Keys in your App.config but you want to get some of them then you should have a specified format for the Application_Name like:

  • application_name1
  • application_name2
  • application_name3
  • application_name4

then do the following in your code then you can add as many buttons you want only by adding the key to the config file.

public void Controls()
{
for (int i = 1; i <= Applications; i++)
{
NameValueCollection sAll;
sAll = ConfigurationManager.AppSettings;

Button button = new Button();

this.Controls.Add(button);
top += button.Height+25;
button.Tag = i;
button.Text = sAll["application_name"+i] ;
}
}

Writing key value pairs into app.config file

The configuration system in .Net was not designed to let you programmatically save changes to the app.config in the assembly folder. I know there is a "Save" method there, but those changes actually save to a copy of the config file. The location of the copy depends on the scope of the setting.

Application Settings such as these:

<appSettings>
<add key="" value=""/>
</appSettings>

Have "application scope" if I recall correctly. If you use the Settings page in the project settings page or open Settings.settings in the Properties folder, you can choose the scope (User or Application).

This way, changes to the settings are persisted for the current user or for that specific version of the application. These copies are stored in a generated folder somewhere in the %APPDATA% for the appropriate account. The configuration system automatically loads the settings again depending on who's logged in.

This is why you also have an "Upgrade" method. It allows you to add settings to new versions of your application and upgrade the user's settings, causing only the new properties to be added to the user's copy. In this way, the setting in the app.config in the assembly folder is only the default value for the setting.

The comments above touch on why this is the way it works: Files inside Program Files are intended to remain untouched after installation and UAC ensures this.

I suggest reading up on the configuration system, it is quite powerful. If you still really want to modify app.config, you have to write custom code to modify the app.config file directly and

  • install into user's profile folders, or
  • always run your program as
    an administrator

Write and read updated appSettings from config file during runtime

You should try

ConfigurationManager.RefreshSection("appSettings");

Found an old post here
Reloading configuration without restarting application using ConfigurationManager.RefreshSection

App.Config change value

You cannot use AppSettings static object for this. Try this

string appPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);          
string configFile = System.IO.Path.Combine(appPath, "App.config");
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

config.AppSettings.Settings["YourThing"].Value = "New Value";
config.Save();

Using values from AppConfig file in C#

I think a better idea is to write a wrapper class to everything that deals with configuration, especially if you write tests. A simple example might be:

public interface IConfigurationService
{
string GetValue(string key);
}

This approach will allow you to mock your configuration when you need it and reduce complexity

So you could proceed with:

public void SelTest(IConfigurationService config)
{
var selenium = new DefaultSelenium(config.GetValue("TestMachine"),
4444, config.GetValue("Browser"), config.GetValue("URL"));
}

or you could inherit your configuration from a List and reduce the typing to:

config["Browser"]


Related Topics



Leave a reply



Submit