最近在搞一個小程序,會用到動態修改配置文件來進行處理,在百度上找了很多辦法,但是始終達不到我預想的效果,先列出程序運行環境和開發工具版本:
開發工具:VS2010
.Net 運行環境:4.0
有兩種方式,分別如下:
第一種方式:只能在程序運行和調試時有效,在程序打包成安裝包並安裝之后會出現問題,完整代碼如下:
/// <summary>
/// 設置配置文件key對應的值
/// </summary>
/// <param name="key">鍵</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool SetAppSetByKey(string key, string value)
{
bool result = false;
try
{
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings[key].Value = value;
cfa.Save();
ConfigurationManager.RefreshSection("appSettings"); //必須刷新
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
/// <summary>
/// 新增配置文件key對應的值
/// </summary>
/// <param name="key">鍵</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool AddAppSetByKey(string key, string value)
{
bool result = false;
try
{
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings.Add(key, value);
cfa.Save();
ConfigurationManager.RefreshSection("appSettings"); //必須刷新
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
第二種方式:能解決以上問題,但是當程序安裝在C盤時會出現配置文件無法訪問的情況,完整代碼如下:
///<summary>
///在config文件中appSettings配置節增加一對鍵、值對,如果已經存在先移除再添加.
///</summary>
///<param name="newKey">新鍵</param>
///<param name="newValue">新值</param>
public static void SetAppSetByKey(string newKey, string newValue)
{
XmlDocument doc = new XmlDocument();
doc.Load(AppConfig());
XmlNode node = doc.SelectSingleNode(@"//appSettings");
XmlElement ele = (XmlElement)node.SelectSingleNode(@"//add[@key='" + newKey + "']");
ele.SetAttribute("value", newValue);
doc.Save(AppConfig());
}
/// <summary>
/// 獲取配置節點
/// </summary>
/// <param name="configName">要獲取的鍵</param>
/// <returns></returns>
public static string GetAppSetByKey(string configName)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(AppConfig());
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings"); //補充,需要在你的app.config 文件中增加一下,<appSetting> </appSetting>
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + configName + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 配置文件根目錄
/// </summary>
/// <returns></returns>
public static string AppConfig()
{
return System.IO.Path.Combine(Application.StartupPath.Trim(), Application.ProductName + ".exe.config");
}
順便提提,方式二,參考文獻來源於:http://www.cnblogs.com/Fooo/archive/2012/12/03/2799714.html
此時已完成一半,接下來處理無法訪問權限的問題:
將程序設置成已管理員身份運行:
如何讓程序在啟動時,自動要求“管理員”權限了,我們只需要修改app.manifest文件中的配置項即可。
app.manifest文件默認是不存在的,我們可以通過以下操作來自動添加該文件。
(1)進入項目屬性頁。
(2)選擇“安全性”欄目。
(3)將“啟用ClickOnce安全設置”勾選上。
現在,在Properties目錄下就自動生成了app.manifest文件,打開該文件,將 trustInfo/security/requestedPrivileges節點的requestedExecutionLevel的level 的值修改為requireAdministrator即可。如下所示:
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> ;
</requestedPrivileges>
(4)記住,如果不需要ClickOnce,可以回到項目屬性頁將“啟用ClickOnce安全設置”不勾選。
(5)接下來,重新編譯你的程序就OK了。
參考文獻來源於:http://www.2cto.com/Article/201112/115471.html
設置后在編譯時會出現一個異常:ClickOnce 不支持請求執行級別"requireAdministrator"...... ——將設置成管理員身份運行步驟里的溝去掉即可
處理以上異常的解決方法參考百度:http://zhidao.baidu.com/link?url=v9xx3nSK8HOES1d0YXoTLRkEACaMmDllyNMz_CNBIP2RSKsFNvHsT7SI5UDrQaqp5c6aJRLAB80HOuoJky0A_a
至此,問題已經解決!!!若有疑問請留言交流,請各位大神多多指點
