標籤:
public class AppModify { /// <summary> /// 依據串連串名字connectionName返回資料連線字串 /// </summary> /// <param name="connectionName"></param> /// <returns></returns> public static string GetConnectionStringsConfig(string connectionName) { string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString(); return connectionString; } /// <summary> /// 更新連接字串 /// </summary> /// <param name="newName"> 連接字串名稱 </param> /// <param name="newConString"> 連接字串內容 </param> /// <param name="newProviderName"> 資料提供者名稱 </param> public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName) { bool isModified = false; // 記錄該串連串是否已經存在 // 如果要更改的串連串已經存在 if (ConfigurationManager.ConnectionStrings[newName] != null) { isModified = true; } // 建立一個連接字串執行個體 ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, newProviderName); // 開啟可執行檔設定檔*.exe.config Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 如果串連串已存在,首先刪除它 if (isModified) { config.ConnectionStrings.ConnectionStrings.Remove(newName); } // 將新的串連串添加到設定檔中. config.ConnectionStrings.ConnectionStrings.Add(mySettings); // 儲存對設定檔所作的更改 config.Save(ConfigurationSaveMode.Modified); // 強制重新載入設定檔的ConnectionStrings配置節 ConfigurationManager.RefreshSection("ConnectionStrings"); } public static void UpdateConnectionStringsConfig(string newName, string newConString) { bool isModified = false; // 記錄該串連串是否已經存在 // 如果要更改的串連串已經存在 if (ConfigurationManager.ConnectionStrings[newName] != null) { isModified = true; } // 建立一個連接字串執行個體 ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString); // 開啟可執行檔設定檔*.exe.config Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 如果串連串已存在,首先刪除它 if (isModified) { config.ConnectionStrings.ConnectionStrings.Remove(newName); } // 將新的串連串添加到設定檔中. config.ConnectionStrings.ConnectionStrings.Add(mySettings); // 儲存對設定檔所作的更改 config.Save(ConfigurationSaveMode.Modified); // 強制重新載入設定檔的ConnectionStrings配置節 ConfigurationManager.RefreshSection("ConnectionStrings"); } /// <summary> /// 返回*.exe.config檔案中appSettings配置節的value項 /// </summary> /// <param name="strKey"></param> /// <returns></returns> public static string GetAppConfig(string strKey) { foreach (string key in ConfigurationManager.AppSettings) { if (key == strKey) { return ConfigurationManager.AppSettings[strKey]; } } return null; } /// <summary> /// 在*.exe.config檔案中appSettings配置節增加一對鍵、值對 /// </summary> /// <param name="newKey"></param> /// <param name="newValue"></param> public static void UpdateAppConfig(string newKey, string newValue) { bool isModified = false; foreach (string key in ConfigurationManager.AppSettings) { if (key == newKey) { isModified = true; } } // Open App.Config of executable Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // You need to remove the old settings object before you can replace it if (isModified) { config.AppSettings.Settings.Remove(newKey); } // Add an Application Setting. config.AppSettings.Settings.Add(newKey, newValue); // Save the changes in App.config file. config.Save(ConfigurationSaveMode.Modified); // Force a reload of a changed section. ConfigurationManager.RefreshSection("appSettings"); } }
AppModify修改app.config