C#之app.config、exe.config和vshost.exe.config作用區別


vshost.exe.config是程序運行時的配置文本 
exe.config是程序運行后會復制到vshost.exe.config 
app.config是在vshost.exe.config和exe.config沒有情況起作用,從app.config復制到exe.config再復制到vshost.exe.config

寫配置文件都是寫到exe.config文件中了,app.config不會變化。 
app.config只在exe.config丟失的情況下在開發環境中重新加載app.config,vshost.exe.config和exe.config會自動創建內容跟app.config一樣。

vshost.exe.config和app.config兩個文件可不要,但exe.config文件不可少。

網絡上有很多文章是講app.config讀寫方法的,可是事實上這個文件程序就不能改變它。

 

     public static void SaveAppSettings(string aKey, string aValue)
        {
            // 創建配置文件對象
            string file = System.Windows.Forms.Application.ExecutablePath;

            //此處修改為.vshost.exe.Config的內容
            //            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);


            if (config.AppSettings.Settings[aKey] != null)
            {
                // 修改
                config.AppSettings.Settings[aKey].Value = aValue;
            }
            else
            {
                // 添加
                AppSettingsSection ass = (AppSettingsSection)config.GetSection("appSettings");
                ass.Settings.Add(aKey, aValue);
            }

            // 保存修改
            config.Save(ConfigurationSaveMode.Modified);

            // 強制重新載入配置文件的連接配置節
            ConfigurationManager.RefreshSection("appSettings");
        }

 


免責聲明!

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



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