Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am making changes in .xml file in his attribute value but its giving error of unauthorized access.

What I have tried:

<pre> try
            {
                string path =Application.StartupPath + "\\OTS.exe.config";
                var doc = new XmlDocument();
                doc.Load(path);
                XmlNodeList endpoints = doc.GetElementsByTagName("connectionStrings");
                foreach (XmlNode item in endpoints)
                {
                    if (item.HasChildNodes)
                    {
                        foreach (var c in item.ChildNodes)
                        {
                            if (((XmlNode)c).NodeType == XmlNodeType.Element)
                            {
                                var adressAttribute = ((XmlNode)c).Attributes["name"];
                                if (adressAttribute.Value.Contains("OTSConnection"))
                                {
                                    ((XmlNode)c).Attributes["connectionString"].Value = connection_string;
                                    //((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbEmbededConnectionProvider;
                                }
                            }
                        }
                    }

                }
                doc.Save(path);    // it is not saving the file. jump to catch block.
                string exePath = Path.Combine(path,"OTS.exe");

                return true;
            }
            catch
            {

                return false;
            }
Posted
Updated 28-May-18 3:19am

1 solution

Normal user accounts have no write access to the program files directories where your application is probably located.

You have to execute it either with administrator priviliges or (preferred) use a different location to store the file.

For application configuration and settings use one of the AppData folders depending on your requirements (local user, roaming user, or all users); see the Application Class (System.Windows.Forms)[^] to get those folders.
 
Share this answer
 
Comments
Member 11776570 28-May-18 11:46am    
Yes, i got that error. There is no access in C:\ to make changes in the file of installed application, that's why it is giving the error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900