[英]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.