XML mode update and get configuration file appSettings node workaround

Source: Internet
Author: User

Recently in a small program, will be used to dynamically modify the configuration file for processing, Baidu found a lot of ways, but always do not achieve the effect I expected, first list the program run environment and development tools version:

Development tools: VS2010

. Net Operating Environment: 4.0

There are two ways of doing this:

The first way: only valid when the program is running and debugging , the problem occurs after the program is packaged and installed, and the complete code is as follows:

<summary>///Set the value for the profile key///</summary>//<param name= "key" > Key </param&gt        ; <param name= "Value" > Value </param>//<returns></returns> public static bool Setapps            Etbykey (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");            Must flush result = true;            } catch (Exception) {result = false;        } return result; }///<summary>///New profile key corresponding Value///</summary>//<param name= "key" > key < /param>//<param name= "value" > 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");            Must flush result = true;            } catch (Exception) {result = false;        } return result; }

The second way: solves the above problem, but when the program is installed on the C drive, the configuration file cannot be accessed , the complete code is as follows:

<summary>////In the config file, add a pair of keys, value pairs, if they already exist, appsettings the configuration section. </summary>///<param name= "NewKey" > New key </param>///<param name= "NewValue" > New value &LT;/PA  ram> 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>//Get Configuration Node///</summary>//<param name= "ConfigName" > Key to get        </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"); Add, you need to add,<appsetting> </appSetting> Xelem = (XmlElement) in your app. config file Xnode.selectsinglen                Ode ("//add[@key = '" + ConfigName + "']");                if (Xelem! = null) return Xelem.getattribute ("value");            else return "";            } catch (Exception) {return ""; }}///<summary>//configuration file root////</summary>//<returns></return s> public static string AppConfig () {return System.IO.Path.Combine (Application.StartupPath.Tr        Im (), Application.productname + ". exe.config"); }

Incidentally, way two, the reference is from: http://www.cnblogs.com/Fooo/archive/2012/12/03/2799714.html

now half done, next deal with the problem of inaccessible permissions:

Set the program to run as Administrator :

How to get the program to automatically ask for "admin" permission when it starts, we just need to modify the configuration item in the App.manifest file.
The app.manifest file does not exist by default, and we can add the file automatically by doing the following.
(1) Enter the project Properties page.
(2) Select the "Security" section.
(3) Tick "Enable ClickOnce security Settings".
Now, the app.manifest file is automatically generated in the properties directory, and the file is opened, and the trustinfo/security/ The value of the requestedexecutionlevel level of the Requestedprivileges node is modified to Requireadministrator. As shown below:
<requestedprivileges xmlns= "Urn:schemas-microsoft-com:asm.v3" >
<requestedexecutionlevel level= "requireadministrator" uiaccess= "false"/>;
</requestedPrivileges>

(4) Remember, if you do not need ClickOnce, you can go back to the project Properties page and uncheck "Enable ClickOnce Security Settings".
(5) Next, recompile your program is OK.

References from: http://www.2cto.com/Article/201112/115471.html

When set, an exception occurs at compile time:ClickOnce does not support request execution level "Requireadministrator" ... --will be set as administrator to run the ditch in the steps to remove the

Solution for handling the above exception reference Baidu: Http://zhidao.baidu.com/link?url=v9xx3nSK8HOES1d0YXoTLRkEACaMmDllyNMz_ Cnbip2rsksfnvhst7si5udrqaqp5c6ajrlab80houojky0a_a

At this point, the problem has been solved!!! If you have any questions, please leave a message to communicate, please the great God more guidance

XML mode update and get configuration file appSettings node workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.