C# update configuration file

using System.Configuration;
static void Main(string[] args)
        {
            UpdateConfigAppSettings();
            ReadConfig();
            Console.ReadLine();
        }


        static void UpdateConfigAppSettings()
        {
            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.AppSettings.Settings["UserName"].Value = "Fred";
            cfg.AppSettings.Settings["UserPwd"].Value = "FredPwd";
            cfg.AppSettings.Settings["IsRemembered"].Value = "True";
            cfg.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("appsettings");
        }

        static void ReadConfig()
        {
            Console.WriteLine($"{ConfigurationManager.AppSettings["UserName"].ToString()}\n" +
                $"{ConfigurationManager.AppSettings["UserPwd"].ToString()}\n" +
                $"{ConfigurationManager.AppSettings["IsRemembered"].ToString()}");
        }

 

C# update configuration file_第1张图片

 

你可能感兴趣的:(C# update configuration file)