ConfigurationManager.RefreshSection("appSettings"); var config = ConfigurationManager.AppSettings; if (config.TryGetValue("keyname", out string value)) { Console.WriteLine($"The value for keyname is {value}"); } else { Console.WriteLine("The keyname was not found."); }
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var section = config.GetSection("sectionname") as ConfigSection; if (section.TryGetValue("keyname", out string value)) { Console.WriteLine($"The value for keyname is {value}"); } else { Console.WriteLine("The keyname was not found."); }In this example, the OpenExeConfiguration method is used to open the configuration file associated with the executing application. Then, the GetSection method is used to retrieve a ConfigSection object that represents the specified section of the configuration file. TryGetValue method is used to retrieve the value of the keyname. If the keyname is found, it is printed to the console, otherwise, an appropriate message is displayed. The System.Configuration package library contains the classes and interfaces that are used to work with configuration files in .NET applications.