-1

New to C#/VS, trying to get a listing of files, feel like this has a simple answer but can;t find.

Basically, wanting to get a file listing, tried various methods but for simplicity, why does

var f = Directory.GetFiles(@"C:\");

returns

System.UnauthorizedAccessException: 'Access to the path 'C:\' is denied.'

Tried running VS as administator, tried on sub folders, tried with sub folders having Everyone with full control permission. Same results.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
wikios
  • 1
  • 1
  • 1
    UAC must be blocking you. Turn it off and it should work unless your on windows 10. On windows 10 you cannot access c drive even with UAC off and local admin account. Even if you copy a text file from your desktop to the c drive it ask for permission. Still havent found a work around this other than using my documents. – Franck Oct 02 '17 at 12:12
  • @Jean-Claude Colette wouldnt that just output the same error just with "The process failed" in front of it? – TheSkimek Oct 02 '17 at 12:26
  • I'm using Windows10. So thats a limitation of the platform? Seems like an extreme limition. – wikios Oct 02 '17 at 12:27
  • 1
    OK's, that lead me to the answer, its a UWP app, and it does seem like what I'm getting is an result of sanboxing. Thanks for feedback. – wikios Oct 02 '17 at 12:32
  • This is the link I found that details the UWP/Win10 issues. https://stackoverflow.com/questions/33082835/windows-10-universal-app-file-directory-access for me I'll have to use something else. – wikios Oct 02 '17 at 12:38
  • @wikios yes windows 10 is very limited. Yesterday i just noticed you are not allowed to create file in the folder where the application was installed even if the application is running as admin. The whole folder by default set all the files as readonly. – Franck Oct 05 '17 at 12:39

2 Answers2

1

In order to access an arbitrary folder from your UWP app, the user first needs to provide consent via the FolderPicker dialog:

FolderPicker picker = new FolderPicker();
picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add("*");
StorageFolder folder = await picker.PickSingleFolderAsync();
var files = await folder.GetFilesAsync();

https://learn.microsoft.com/en-us/uwp/api/Windows.Storage.Pickers.FolderPicker

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
0

System.UnauthorizedAccessException: 'Access to the path 'C:\' is denied.'

Rob has said Windows Store apps run sandboxed and have very limited access to the file system in his blog. Access to other locations is available only through a broker process. This broker process runs with the user’s full privileges. So you could access the folder that mentioned by Stefan Wick with full privileges FolderPicker.

Apps can access certain file system locations by default. Apps can also access additional locations by declaring capabilities.

There are some locations that all apps can access. Such as InstalledLocation LocalFolder.

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

In addition to the default locations, an app can access additional files and folders by declaring capabilities in the app manifest. Such as Pictures Music library.

<Capabilities><uap:Capability Name="musicLibrary"/></Capabilities>

<Capabilities><uap:Capability Name="picturesLibrary"/></Capabilities>

For more, you could refer to File access permissions. And Skip the path: stick to the StorageFile will help you understand uwp file access in depth.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36