xml 方式更新和获取 配置文件 appSettings 节点 解决办法

最近在搞一个小程序,会用到动态修改配置文件来进行处理,在百度上找了很多办法,但是始终达不到我预想的效果,先列出程序运行环境和开发工具版本:

开发工具: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

至此,问题已经解决!!!若有疑问请留言交流,请各位大神多多指点

在旅途中,我遇见了你,你我相识是缘分!看着你手中的戒指,

xml 方式更新和获取 配置文件 appSettings 节点 解决办法

相关文章:

你感兴趣的文章:

标签云: