在運行時將寫訪問,我需要改變app.config文件我如何使用C#使用C#在運行時
Q
使用C#在運行時
0
A
回答
2
你需要一個參考System.Configuration
添加到您的項目做到這一點。然後,你可以使用這樣的代碼來修改你的可執行程序的app.config:
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Remove("LastDateFeesChecked");
config.AppSettings.Settings.Add("LastDateFeesChecked", DateTime.Now.ToShortDateString());
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
注意:此代碼似乎不會在調試工作。您必須在「發佈模式」中運行代碼才能使其工作。
這是CodeProject上的promising link。
1
不知道,但嘗試這個
設定基準來命名空間
using System.Configuration;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection configSection = config.AppSettings;
try {
if (configSection != null) {
if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false) {
configSection.Settings("KeyName").Value = "NewValue";
config.Save();
}
}
}
catch (ConfigurationException ex) {
MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
0
啊..你不知道。你的問題可能被解釋爲「我不願意寫它」。這個是正常的。應用程序文件夾不被程序或普通用戶編輯。將您的非靜態配置存儲在其他位置(CommonAppData特殊文件夾)。
相關問題
- 1. 在運行時使用c#像腳本
- 2. 在C#中使用在運行時確定的C++ DLL
- 3. Objective-C運行時庫使用
- 4. 運行時錯誤使用C
- 5. 在運行時使用`EditorSceneManager.SaveScene`
- 6. 使用Class.forName在運行時
- 7. C++程序在運行時
- 8. 在運行時確定(C#)
- 9. 麻煩添加新行到datagridview在運行時使用c#
- 10. 在運行時僅使用C++更改用戶界面(無QML)
- 11. 在C#運行時使用反射調用ToList()方法#
- 12. C#通行證泛型在運行時
- 13. 運行在C/C#
- 14. Sqldeveloper在運行時使用java在Fedora上運行erorr
- 15. C#在運行時使用泛型創建對象
- 16. 在C#中使用PowerShell運行命令時訪問被拒絕
- 17. C#Windows服務在使用特定域帳戶時不運行
- 18. 在Unity中使用c#移動運行時創建的對象
- 19. 運行時異常與輸入使用scanf或獲取在C
- 20. C#重寫object.ToString()在運行時使用反射
- 21. 確定在C中使用makefile時將運行哪些命令?
- 22. 在C#中運行時使用外部resx文件(不嵌入)
- 23. C#類型在運行時使用反射鑄造
- 24. 運行OpenCV時在Mac OS中使用Objective-C發出警告
- 25. 使用C++在Solaris上查詢運行時堆棧大小
- 26. 如何在winforms c中使用長時間運行的進程#
- 27. 使用C++在運行時生成聲音
- 28. 使用C#在Selenium.Webdriver + PhantomJS中運行時更改代理
- 29. 運行在C
- 30. 使用CSharpCodeProvider進行C#運行時編譯
你想同樣的應用程序app.config? – 2011-04-09 12:28:31