“访问路径......被拒绝”(.NET C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1450951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Access to the path ... is denied" (.NET C#)
提问by MrGreggles
I've been saving a small XML data file to an external drive, no probs. But then I tried to use the ApplicationData folder and others, even C:\ but no luck. I'm getting an error like "Access to the path "C:\" denied".
我一直在将一个小的 XML 数据文件保存到外部驱动器,没有问题。但是后来我尝试使用 ApplicationData 文件夹和其他文件夹,甚至 C:\ 但没有运气。我收到类似“访问路径“C:\”被拒绝”的错误消息。
Just to confirm, the file is created and read fine with the current code, to an external drive. I guess this is something to do with security & permissions but I haven't found anything too useful.
只是为了确认,文件已创建并使用当前代码读取到外部驱动器。我想这与安全和权限有关,但我没有发现任何有用的东西。
Thanks in advance if you can point me in the right direction on this one!
如果您能指出我正确的方向,请提前致谢!
string fipData = @"F:\IL2\SIIYM\SIIYM Data.xml"; // external drive ok :-)
//string fipData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//string fipData = @"C:\";
// if the XML data file doesn't exist, create it
bool dataFileExists = File.Exists(fipData);
if (dataFileExists)
{
// read the XML values
XDocument xData = XDocument.Load(fipData);
//...
}
else
{
// create & save the XML data file
XElement xLastPath = new XElement(el_lastPath, "");
XElement xLastCode = new XElement(el_lastCode, "");
XElement xRoot = new XElement(el_root);
xRoot.Add(xLastPath);
xRoot.Add(xLastCode);
XDocument newDataFile = new XDocument();
newDataFile.Add(xRoot);
try
{
newDataFile.Save(fipData);
}
catch (Exception ex)
{
MessageBox.Show("Data file unable to be created. System message:{0}".Put(Environment.NewLine + Environment.NewLine + ex.Message));
}
}
回答by Adam Ralph
I can only imagine that the application must be running in the context of a user which does not have access to the local drive, e.g. an ASP.NET website running under the anonymous IIS account or a service account which only has access to the relevant network locations.
我只能想象应用程序必须在无法访问本地驱动器的用户的上下文中运行,例如在匿名 IIS 帐户或只能访问相关网络的服务帐户下运行的 ASP.NET 网站地点。
回答by Heiko Hatzfeld
Most likely the external drive is formated with FAT. FAT does not support rights management for users, so saving there is ok.
很可能外部驱动器是用 FAT 格式化的。FAT 不支持用户权限管理,保存即可。
Besides that the IIS User has no rights to the other folders like Adam mentioned already
除此之外,IIS 用户对 Adam 已经提到的其他文件夹没有任何权限
回答by blowdart
In the comments to another answer you say this is a desktop application, so lets treat each location separately.
在对另一个答案的评论中,您说这是一个桌面应用程序,因此让我们分别处理每个位置。
Under Vista and beyond, an ordinary user does not have rights to create files in the root directory of the system drive (usually C:). You can see this for yourself by opening C:\ in explorer, right clicking and trying to create a file - you should get a UAC prompt. So if you want to write to C:\ then your application needs to run as an administrator, via a suitable manifest demanding elevation, or by starting a separate process when you want to write to that location.
在 Vista 及更高版本下,普通用户无权在系统驱动器的根目录(通常为 C:)中创建文件。您可以通过在资源管理器中打开 C:\,右键单击并尝试创建文件来亲眼看到这一点 - 您应该会得到一个 UAC 提示。因此,如果您想写入 C:\,那么您的应用程序需要以管理员身份运行,通过合适的清单要求提升,或者在您想要写入该位置时启动一个单独的进程。
Application Data, Environment.SpecialFolder.ApplicationData should however work. If you output the actual directory that returns what do you get?
应用程序数据,Environment.SpecialFolder.ApplicationData 应该可以工作。如果您输出返回什么的实际目录,您会得到什么?