在C#中未经授权访问文件时出错 [英] Error to unauthorized access of file in C#
本文介绍了在C#中未经授权访问文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在.xml文件中更改了他的属性值,但是它给出了未经授权访问的错误。
我尝试了什么:
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;
}
推荐答案
普通用户帐户对您的应用程序可能位于的程序文件目录没有写入权限。 />
您必须使用管理员权限执行此操作,或者(首选)使用其他位置来存储文件。
对于应用程序配置和设置,请根据您的要求使用 AppData 文件夹之一(本地用户,漫游用户或所有用户);请参阅应用程序类(System.Windows.Forms) [ ^ ]。
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.
这篇关于在C#中未经授权访问文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文