0

I have config file for my app, my app.config is as below

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"    />
  </configSections>
  <appSettings>
    <add key="log4net.Config" value="log4netConfig.xml" />
    <add key="proxyaddress" value="192.168.130.5"/>
  </appSettings>
  <system.net>
    <defaultProxy enabled ="true" useDefaultCredentials = "true">
        <proxy autoDetect="false"  
            bypassonlocal="true" 
            proxyaddress="192.168.130.6"
            scriptLocation="https://ws.mycompany.com/proxy.dat" 
            usesystemdefault="true" 
        />
    </defaultProxy>
  </system.net>
</configuration>

var proxy= ConfigurationManager.AppSettings["proxyaddress"]; 

will get "192.168.130.5",

How to get all proxy settings in system.net section in c#?

Updated: I changed my config to the following and get it:

string proxyURLAddr = ConfigurationManager.AppSettings["proxyaddress"];

Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
  </configSections>
  <appSettings>
    <add key="log4net.Config" value="log4netConfig.xml" />
    <add key="proxyaddress" value=""/>
  </appSettings>
  <system.net>
    <defaultProxy enabled ="true" useDefaultCredentials = "true">
      <proxy usesystemdefault ="True" bypassonlocal="False"/>
    </defaultProxy>
  </system.net>
</configuration>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
toosensitive
  • 2,335
  • 7
  • 46
  • 88

2 Answers2

0

Update 2:

You can read the values like described here but for local app settings configuration file:

var proxy = System.Configuration.ConfigurationManager.GetSection("system.net/defaultProxy") as System.Net.Configuration.DefaultProxySection  
if (proxy != null) 
{ /* Check Values Here */ }

For custom sections you can use the following steps:

You have define a custom configsection class derived from ConfigurationSection:

public class ProxyConfiguration : ConfigurationSection
{
    private static readonly ProxyConfiguration Config = ConfigurationManager.GetSection("proxy") as ProxyConfiguration;

    public static ProxyConfiguration Instance
    {
        get
        {
            return Config;
        }
    }

    [ConfigurationProperty("autoDetect", IsRequired = true, DefaultValue = true)]
    public bool AutoDetect
    {
        get { return (bool)this["autoDetect"]; }
    }
  // all other properties
}

After that you can use the class instance to access the values:

ProxyConfiguration.Instance.AutoDetect

You can find an example in MSDN

Community
  • 1
  • 1
edesr
  • 200
  • 1
  • 1
  • 12
  • Thanks. but ConfigurationManager.GetSection("proxy") is null – toosensitive Feb 18 '16 at 16:54
  • var proxy = System.Web.Configuration.WebConfigurationManager.GetSection("system.net/defaultProxy") as System.Net.Configuration.DefaultProxySection – edesr Feb 18 '16 at 19:11
  • I'm sorry I have misread your question. For app settings it's System.Configuration.ConfigurationManager. I tried this and was able to read the values. – edesr Feb 19 '16 at 10:08
0

toosensitive, your app.config when built and deployed will be renamed to : name of of your assembly + (.exe or .dll) + ".config". The answer above is valid for web applications, but not for console ones, libraries and windows services. You can't put app.config alongside with any assembly and expect this assembly to start reading the appSettings section, the same way IIS reads web.config files. I think this is why you receive null.