0

I added keys with values to my config file and after some web-search i found out how to import those values into my .cs file for later use. At the moment i put the value into the content attribute of a label.

When i run the program the label is empty. That means that something must have happened to it because i statically inserted some text in the XAML.

This is the config:

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
        <add key="SourcepathClient" value="D:\xxx"/>
        <add key="SourcepathWin32" value="D:\xxx"/>
        <add key="DestinationpathUpdatePackages" value="D:\xxx"/>
        <add key="DestinationpathClient" value="D:\xxx"/>
    </appSettings>
</configuration>

and this the .cs file:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    LabelSourcepath.Content = System.Configuration.ConfigurationManager.AppSettings["SourcepathClient‌​"];
}

I dont get any error, simply the value isnt shown in my label.

MansNotHot
  • 263
  • 1
  • 3
  • 19
  • Why don't you simply bind to application settings? As e.g. shown here: https://stackoverflow.com/a/263956, or here: https://stackoverflow.com/a/845033 – Clemens Oct 03 '17 at 09:35

2 Answers2

1

You may want to read this:

ConfigurationManager.AppSettings in returning null

You could also try to get the setting by index:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    LabelSourcepath.Content = System.Configuration.ConfigurationManager.AppSettings[0];
}
mm8
  • 163,881
  • 10
  • 57
  • 88
-1

Here an example:

in config file:

<appSettings>     
<add key="xpsPath" value="C:\Users\yourUser\Desktop\yourFolder"/> 
</appSettings>

in cs file:

string path = ConfigurationManager.AppSettings["xpsPath"];

Then just put the string in the label.