简体   繁体   English

创建新目录将返回错误:“ System.UnauthorizedAccessException:'对路径'C:\\ Users'的访问被拒绝。

[英]Creating new directory returns error :"System.UnauthorizedAccessException : 'Access to the path 'C:\Users' is denied.'

I'm making a small UWP program which will have a config.xml file. 我正在制作一个小型的UWP程序,它将具有config.xml文件。 I want this one to be saved in the "My Games\\My application\\" folder. 我希望将其保存在“我的游戏\\我的应用程序\\”文件夹中。

This is the code I use : 这是我使用的代码:

string folder = "C:\\Users\\admin\\Documents\\My Games\\MyAppFolder";
Directory.CreateDirectory(folder);

When running the program, the second line gives this error : 运行程序时,第二行给出此错误:

"System.UnauthorizedAccessException : 'Access to the path 'C:\\Users' is denied.'". “ System.UnauthorizedAccessException:'对路径'C:\\ Users'的访问被拒绝。”

I tried with 我尝试过

string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\My Games\\MyAppFolder"

and

string folder = "C:\\Users\\admin\\Documents\\MyAppFolder";

and got the same issue. 并遇到了同样的问题。

However, I tried with a C# winforms application where I didn't have any issue : there, the same code works perfectly, the new folder is created. 但是,我尝试了一个C# winforms应用程序,该应用程序没有任何问题:在那里,相同的代码运行良好,创建了新文件夹。

Thanks for help! 感谢帮助!

Creating new directory returns error :"System.UnauthorizedAccessException : 'Access to the path 'C:\\Users' is denied.' 创建新目录将返回错误:“ System.UnauthorizedAccessException:'对路径'C:\\ Users'的访问被拒绝。

UWP is running on sandbox, you could not use System.IO api( Directory.CreateDirectory ) to access folder path directly, and you need use Windows Storage api to access the specific folder. UWP在沙盒上运行,您不能使用System.IO api( Directory.CreateDirectory )直接访问文件夹路径,而需要使用Windows Storage api来访问特定文件夹。 For access such path C:\\\\Users\\\\admin\\\\Documents\\\\MyAppFolder , you also add broadFileSystemAccess capability and open this permission in the file setting. 为了访问这样的路径C:\\\\Users\\\\admin\\\\Documents\\\\MyAppFolder ,您还添加了broadFileSystemAccess功能并在文件设置中打开此权限。 For more please refer this case reply 有关更多信息,请参阅此案例回复

<Package
  ...
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp uap5 rescap">
...
<Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>

暂无
暂无

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

相关问题 C# 对路径的访问被拒绝...(System.UnauthorizedAccessException:对路径'C:\'的访问被拒绝。) - C# access to the path is denied…(System.UnauthorizedAccessException: Access to the path 'C:\' is denied.) File.ReadAllText(): System.UnauthorizedAccessException: '访问路径被拒绝。' - File.ReadAllText(): System.UnauthorizedAccessException: 'Access to the path is denied.' C#-UWP-System.UnauthorizedAccessException:“访问被拒绝”。 - C# - UWP - System.UnauthorizedAccessException: 'Access is denied.' System.UnauthorizedAccessException:访问路径拒绝错误 - System.UnauthorizedAccessException: Access to the path denied error System.UnauthorizedAccessException:访问路径:拒绝PATH - System.UnauthorizedAccessException: Access to the path:PATH is denied System.UnauthorizedAccessException:拒绝访问路径 (UWP C#) - System.UnauthorizedAccessException: Access to the path is denied (UWP C#) Visual Studio 2019:System.UnauthorizedAccessException:“访问路径“C:/”被拒绝 - Visual Studio 2019: System.UnauthorizedAccessException: "Access to the path "C:/" was denied UWP System.UnauthorizedAccessException:拒绝访问路径 - UWP System.UnauthorizedAccessException: Access to the path is denied System.UnauthorizedAccessException:拒绝访问路径“...” - System.UnauthorizedAccessException: Access to the path "..." is denied DotNetZip System.UnauthorizedAccessException:拒绝访问路径 - DotNetZip System.UnauthorizedAccessException: Access to the path is denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM