Search notes:

C-Sharp: read settings from an App.exe.config file

//
//  csc App.cs
using System;
using System.Configuration;

namespace TQ84  {

  class App  {

    static void Main(string[] args)  {
    //
    // Read App.exe.config
    //
       System.Collections.Specialized.NameValueCollection settings = ConfigurationManager.AppSettings;

    //
    // Get specific values:
    //
       Console.WriteLine("num = " + settings["num"] ?? "n/a");
       Console.WriteLine("txt = " + settings["txt"] ?? "n/a");
       Console.WriteLine("bla = " + settings["bla"] ?? "n/a");

    //
    // Iterate over all values
    //
       Console.WriteLine("--------------");
       Console.WriteLine("There are {0} setting(s) in App.exe.config:", settings.Count);
       foreach (var key in settings.AllKeys)  {
            Console.WriteLine("  {0} = {1}", key, settings[key]);
       }
    }
  }
}
Github repository .NET-API, path: /System/Configuration/ConfigurationManager/read-settings/App.cs

Sample config file

The config file must be named app-name.exe.config, for this example App.exe.config:
<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  

  <startup>   
      <supportedRuntime
        version="v4.0"
        sku    =".NETFramework,Version=v4.5"
      />  
  </startup>  

  <appSettings>  

      <add key="num"  value="42"          />  
      <add key="txt"  value="Hello World" />  

  </appSettings>  

</configuration> 
Github repository .NET-API, path: /System/Configuration/ConfigurationManager/read-settings/App.exe.config.github

Creating and running executable

We use the C-Sharp compiler csc.exe to turn the source code App.cs into an executable.
If there is also an App.exe.config file, we remove it:
rm   App.exe        -errorAction ignore
rm   App.exe.config -errorAction ignore

csc  -nologo App.cs
Github repository .NET-API, path: /System/Configuration/ConfigurationManager/read-settings/compile.ps1
Running the exe with
mv App.exe.config.github App.exe.config
write-host -foreGroundColor green 'execute app with App.exe.config'
& .\App
Github repository .NET-API, path: /System/Configuration/ConfigurationManager/read-settings/run-with.ps1
… and without the config file:
mv App.exe.config.github App.exe.config
write-host -foreGroundColor green 'execute app with App.exe.config'
& .\App
Github repository .NET-API, path: /System/Configuration/ConfigurationManager/read-settings/run-with.ps1

See also

The system.Configuration.ConfigurationManager class.
C-Sharp: modify an App.exe.config file

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1759533205, '216.73.216.149', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/dot-net/namespaces-classes/System/Configuration/ConfigurationManager/read-settings(127): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78