Access C Drive files in UWP AppService

跟風遠走 提交于 2019-12-10 17:54:17

问题


How to access the system drive(C, D drive) files in UWP AppService. Example: I want to access "C:\Test\sample.txt" file from UWP AppServices.

I have tried the below code. but throwing error(Additional information: Access is denied.). And also added the "Removable Storage" Capabalities in appxmanifest file.

StorageFolder testfolder = await StorageFolder.GetFolderFromPathAsync(@"c:\\test");
StorageFile sourcefile = await testfolder.GetFileAsync("sample.txt");
StorageFile destinationfile = await KnownFolders.SavedPictures.CreateFileAsync("Mysample.txt");
using (var sourcestream = (await sourcefile.OpenReadAsync()).GetInputStreamAt(0))
{
    using (var destinationstream = (await destinationfile.OpenAsync(FileAccessMode.ReadWrite)).GetOutputStreamAt(0))
    {
        await RandomAccessStream.CopyAndCloseAsync(sourcestream, destinationstream);
    }
}

回答1:


I appreciate it's late, but for anyone else refering to this post now, it appears Microsoft have now added the capability.

Simply add the 'broadFileSystemAccess' capability to the app manifest as described here: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions

Note that this still requires user input to some extent (the user must grant permission to file system access on first run of the app) but that the file/folder picker UI is not needed.

I haven't actually tried this yet but it sounds what you are after.




回答2:


Without user interaction, you can only open specific locations (see https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions). As mentioned by @TheTanic, you can access other locations only with user interaction (FileOpenPicker/FolderPicker). Of course, for a "pure" UWP AppService, that is a problem.

Here's an approach (I won't even call it a solution) that works only for a very narrow set of scenarios: If...

  • you know that all files are below a specific folder and
  • it's OK for you have at least some kind of (minimal) UI

then you can do the following:

  • Host the AppService in a UWP application (see https://docs.microsoft.com/en-us/windows/uwp/launch-resume/convert-app-service-in-process)
  • Ask the user once for a folder when starting the app for the first time (using FolderPicker)
  • Store that folder in a FutureAccessList (see https://docs.microsoft.com/en-us/uwp/api/Windows.Storage.AccessCache.StorageApplicationPermissions#Windows_Storage_AccessCache_StorageApplicationPermissions_FutureAccessList) under a specific name
  • The next time the application hosting the AppService starts, you can get the folder from the FutureAccessList

Of course, in practice you'd access the FutureAccessList each time the application is started, and if it doesn't contain the folder, then ask the user.



来源:https://stackoverflow.com/questions/39873024/access-c-drive-files-in-uwp-appservice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!