简体   繁体   English

拒绝访问路径****

[英]Access to path **** is denied

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. 我有一个C#程序,该程序已构建XML文档,并试图将其保存到MyDocuments中的文件夹中。 I am getting the folliwing exception when calling the XMLDoc.Save function. 调用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. 我有以管理员身份运行的Visual Studio。 Any thoughts on how to fix it? 关于如何解决它的任何想法?

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. 感谢您的所有帮助/评论。

There are a few possibilities: 有几种可能性:

  • ConfigOutput is a folder ConfigOutput是一个文件夹
  • ConfigOutput is a file that is in use (opened) ConfigOutput是一个正在使用(打开)的文件
  • You're not logged in as User 'Ash' 您尚未以“ Ash”用户身份登录

You should not normally have to run as Admin to write to your own Documents folder. 通常,您不必以管理员身份运行即可写入自己的Documents文件夹。

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
}

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

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应用程序以管理员身份运行?

PS Check if your file is not already opened by another FileStream or etc. PS检查您的文件是否尚未被其他FileStream等打开。

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. 我不知道这是否有所不同,但是您可能想以相对而非绝对的方式指定文件夹: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)将为您提供当前用户文档的路径。 Check if it's different from the one you provide. 检查它是否与您提供的不同。

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调试应用程序时,documents文件夹与我当前登录并在其下运行Visual Studio的用户相同。

You could try <requestedExecutionLevel level="asInvoker" uiAccess="true|false"/> first and progressively move to highestAvailable and requireAdministrator . 您可以先尝试<requestedExecutionLevel level="asInvoker" uiAccess="true|false"/>然后逐步移至highestAvailablerequireAdministrator

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());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM