简体   繁体   English

如何在 c# uwp 中没有对话框的情况下将文件保存在计算机的任何路径上

[英]How to save a file on any path of computer without dialog in c# uwp

I'm trying to save a file without a filepicker under my desktop or somewhere else, but I always get the error "Access to path [...] is denied"我试图在我的桌面或其他地方保存一个没有文件选择器的文件,但我总是收到错误“访问路径 [...] 被拒绝”

This is my code:这是我的代码:

    private async void SaveFile(string Path)
    {
        StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Path);
        StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
    }

I tried the same code but with the Localfoder and it worked.我尝试了相同的代码,但使用 Localfoder 并且它有效。

Is there any way to get access to the path without a filepicker?有没有办法在没有文件选择器的情况下访问路径?

Using FilePicker gets permission from the user to access file locations.使用 FilePicker 从用户那里获得访问文件位置的权限。 File system access is a restricted capability in UWP.文件系统访问是 UWP 中的受限功能。 You can enable Broad Filesystem Access in your package manifest to get this permission when the app is installed.您可以在 package 清单中启用广泛的文件系统访问,以便在安装应用程序时获得此权限。 It's not recommended for apps published to the Microsoft Store, because it makes it less likely for you app to be approved.不建议将其用于发布到 Microsoft Store 的应用,因为这会降低您的应用获得批准的可能性。 See the App capability declarations documentation.请参阅应用功能声明文档。

Alternatively, you could run your app from a folder that contains the desktop in one of its subfolders by using the AppExecutionAlias extension mention under Accessing additional locations in File Access Permissions .或者,您可以使用在文件访问权限访问其他位置下提到的 AppExecutionAlias 扩展名从包含桌面的文件夹中运行您的应用程序。 Underneath that section there is also an explanation of Broad Filesystem Access as well.在该部分的下面还有对广泛文件系统访问的解释。

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

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