![](/img/trans.png)
[英]C# access to the path is denied…(System.UnauthorizedAccessException: Access to the path 'C:\' is denied.)
[英]Creating new directory returns error :"System.UnauthorizedAccessException : 'Access to the path 'C:\Users' is denied.'
我正在制作一個小型的UWP
程序,它將具有config.xml
文件。 我希望將其保存在“我的游戲\\我的應用程序\\”文件夾中。
這是我使用的代碼:
string folder = "C:\\Users\\admin\\Documents\\My Games\\MyAppFolder";
Directory.CreateDirectory(folder);
運行程序時,第二行給出此錯誤:
“ System.UnauthorizedAccessException:'對路徑'C:\\ Users'的訪問被拒絕。”
我嘗試過
string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\My Games\\MyAppFolder"
和
string folder = "C:\\Users\\admin\\Documents\\MyAppFolder";
並遇到了同樣的問題。
但是,我嘗試了一個C#
winforms
應用程序,該應用程序沒有任何問題:在那里,相同的代碼運行良好,創建了新文件夾。
感謝幫助!
創建新目錄將返回錯誤:“ System.UnauthorizedAccessException:'對路徑'C:\\ Users'的訪問被拒絕。
UWP在沙盒上運行,您不能使用System.IO
api( Directory.CreateDirectory
)直接訪問文件夾路徑,而需要使用Windows Storage
api來訪問特定文件夾。 為了訪問這樣的路徑C:\\\\Users\\\\admin\\\\Documents\\\\MyAppFolder
,您還添加了broadFileSystemAccess
功能並在文件設置中打開此權限。 有關更多信息,請參閱此案例回復
<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.