通過本地exe.config進行配置並修改


//本機config操作
 Configuration configs = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configs.AppSettings.Settings.Remove("Version_Number");
                configs.AppSettings.Settings.Add("Version_Number", newNumber);
                //一定要記得保存,寫不帶參數的config.Save()也可以
                configs.Save();
                //刷新,否則程序讀取的還是之前的值(可能已裝入內存)
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
//操作其他程序的config
                string configPath = Application.StartupPath + @"\WindowsFormsApp1.exe.config";//獲取到主程序的config,進行修改版本
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = configPath;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                config.AppSettings.Settings.Remove("Version_Number");
                config.AppSettings.Settings.Add("Version_Number", newNumber);
                //一定要記得保存,寫不帶參數的config.Save()也可以
                config.Save();
                //刷新,否則程序讀取的還是之前的值(可能已裝入內存)
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="Version_Number" value="1"/>
  </appSettings>
</configuration>
View Code

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM