C# 访问路径 **** 被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10650528/
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 path **** is denied
提问by user589195
I know there is a ton of stuff on this already and have tried a few things but have had no luck in fixing it.
我知道这方面已经有很多东西了,并且已经尝试了一些东西,但没有成功修复它。
I have a C# program that has built an XML Document and im trying to save it to a folder thats in MyDocuments. I am getting the folliwing exception when calling the XMLDoc.Savefunction.
我有一个 C# 程序,它构建了一个 XML 文档,我试图将它保存到 MyDocuments 中的文件夹中。调用该XMLDoc.Save函数时出现以下异常。
Access to the path 'C:\Users\Ash\Documents\ConfigOutput' is denied
访问路径“C:\Users\Ash\Documents\ConfigOutput”被拒绝
I have visual studio running as administrator. Any thoughts on how to fix it?
我以管理员身份运行 Visual Studio。关于如何解决它的任何想法?
I have tried saving onto the desktop and into the C:\ folder aswell.
我曾尝试保存到桌面和 C:\ 文件夹中。
I am using windows 7.
我正在使用 Windows 7。
Running the built executable also doesnt seem to work.
运行构建的可执行文件似乎也不起作用。
Apologies guys it seems I was being stupid. I had indeed not added a filename to the output path. I'll not delete the question incase anyone else gets done by this gotcha! Thanks for all the help/comments.
抱歉各位,看来我是傻了。我确实没有在输出路径中添加文件名。我不会删除这个问题,以防其他人被这个问题搞砸了!感谢所有的帮助/评论。
采纳答案by Henk Holterman
There are a few possibilities:
有几种可能:
- ConfigOutput is a folder
- ConfigOutput is a file that is in use (opened)
- You're not logged in as User 'Ash'
- ConfigOutput 是一个文件夹
- ConfigOutput 是一个正在使用(打开)的文件
- 您未以用户“Ash”身份登录
You should not normally have to run as Admin to write to your own Documents folder.
您通常不必以管理员身份运行来写入您自己的 Documents 文件夹。
回答by sickUnit
Try run your app as administrator.
尝试以管理员身份运行您的应用程序。
If you want to debug your app, start your Visual Studio as administrator also.
如果你想调试你的应用程序,也以管理员身份启动你的 Visual Studio。
To force app start as administrator take a look at this thread: How do I force my .NET application to run as administrator?
要强制以管理员身份启动应用程序,请查看此线程: 如何强制我的 .NET 应用程序以管理员身份运行?
P.S. Check if your file is not already opened by another FileStream or etc.
PS 检查您的文件是否尚未被另一个 FileStream 等打开。
回答by Davio
I don't know if this makes a difference, but you may want to specify the folder in a relative rather than absolute manner: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)will provide you with the path of the current user's documents. Check if it's different from the one you provide.
我不知道这是否有区别,但您可能希望以相对方式而不是绝对方式指定文件夹:Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)将为您提供当前用户文档的路径。检查它是否与您提供的不同。
If so, you may need to run the app under a different user or administrator as others have pointed out. Obviously one user isn't allowed to just save files into another user's documents folder.
如果是这样,您可能需要像其他人指出的那样,在不同的用户或管理员下运行该应用程序。显然,不允许一个用户将文件保存到另一个用户的文档文件夹中。
For me when I debug my app from Visual Studio the documents folder is the same as the user I'm currently logged in as and running Visual Studio under.
对我来说,当我从 Visual Studio 调试我的应用程序时,文档文件夹与我当前登录并在其下运行 Visual Studio 的用户相同。
You could try <requestedExecutionLevel
level="asInvoker"
uiAccess="true|false"/>first and progressively move to highestAvailableand requireAdministrator.
您可以<requestedExecutionLevel
level="asInvoker"
uiAccess="true|false"/>先尝试并逐步移动到highestAvailable和requireAdministrator。
Alternatively, demand the permissions, catch the exception and print it out:
或者,请求权限,捕获异常并将其打印出来:
try {
FileIOPermission fileIOPermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, myDocFolderFile);
fileIOPermission.Demand();
} catch (SecurityException se) {
Debug.WriteLine(se.ToString());
}
回答by FosterZ
You need to check and get permission to that directory/file your writing.. for that
use Security namesapce
您需要检查并获得对该目录/文件的许可,以便使用 Security namesapce
var permissionSet = new PermissionSet(PermissionState.None);
var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, pathToFolder);
permissionSet.AddPermission(writePermission);
if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
{
// do your stuff
}
else
{
// alternative stuff
}
回答by Rich
It looks like you're not specifying a filename and therefore it can't create a file with the same name as an existing directory - so try changing your path to:
看起来您没有指定文件名,因此它无法创建与现有目录同名的文件 - 因此尝试将路径更改为:
C:\Users\Ash\Documents\ConfigOutput\Out.xml
C:\Users\Ash\Documents\ConfigOutput\Out.xml

