Suresh K
Suresh K

Reputation: 131

Access C Drive files in UWP AppService

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);
    }
}

Upvotes: 3

Views: 2747

Answers (2)

arneyjfs
arneyjfs

Reputation: 460

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://learn.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.

Upvotes: 4

Roland Weigelt
Roland Weigelt

Reputation: 123

Without user interaction, you can only open specific locations (see https://learn.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:

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.

Upvotes: 2

Related Questions