Howto initialize a variable of type StorageFolder with individual path?

0

I am trying to get all the files properties, that the Windows Explorer properties / details Tab shows on my Windows 10 Desktop.

I am following the samples from that article: https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-getting-file-properties

I have seen that there are functions to get basic and extended file properties.

The basic sample code looks like this:

var folder = Windows.Storage.KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

foreach (Windows.Storage.StorageFile file in files)
{
    StringBuilder fileProperties = new StringBuilder();

    // Get top-level file properties.
    fileProperties.AppendLine("File name: " + file.Name);
    fileProperties.AppendLine("File type: " + file.FileType);
}

I have managed to find out required additional steps to make it work like implementation of async method and adjust settings to access picture library. It works.

Now I would like to change the code in a way that it does not run for the Picture Library but instead in any folder I would like to hardcode in a first step and make selectable by users in a second step.

Unfortunately I fail. How do I have to adjust the code to make it use a path like "c:\test" ?

The query method of the code uses a StorageFolder object.

What I tried:

The "good old"

var di = new System.IO.DirectoryInfo("C:\test");

does not create a type that seems somehow compatible with a StorageFolder. This does not work:

Windows.Storage.StorageFolder f = (StorageFolder)di;

That does not work either:

Windows.Storage.StorageFolder f = new StorageFolder();

Then I found this working option:

var folder1 = Windows.Storage.StorageFolder.GetFolderFromPathAsync("c:\test"); 

But then the folder.CreateFileQuery() method is not happy with it and denies to work.

Next try after reading suggested similar questions:

try
{
    folder = await folder.GetFolderAsync(folderName);
}
catch (Exception)
{
    await folder.CreateFolderAsync(folderName);
}

Leads into this exception:

System.ArgumentException HResult=0x80070057 Nachricht = Value does not fall within the expected range. Quelle = System.Private.CoreLib

I am sure this is an easy task, but I can't find a solution to make the samples work with a specific folder on my hard disk.

Thanks.

c#
.net
uwp
asked on Stack Overflow Jun 11, 2020 by Magier • edited Jun 11, 2020 by marc_s

1 Answer

1

How do I have to adjust the code to make it use a path like "c:\test"

If you want to access the folder in full path, first you need to add broadFileSystemAccess capability in your manifest and then access is configurable in Settings > Privacy > File system to allow your app to access file system. After that, you can pass the folder path to GetFolderFromPathAsync method to get the specified folder.

var folder1 = await StorageFolder.GetFolderFromPathAsync(@"c:\test");
var query = folder1.CreateFileQuery();
var files = await query.GetFilesAsync();

foreach (Windows.Storage.StorageFile file in files)
{
    StringBuilder fileProperties = new StringBuilder();

    // Get top-level file properties.
    fileProperties.AppendLine("File name: " + file.Name);
    fileProperties.AppendLine("File type: " + file.FileType);
}
answered on Stack Overflow Jun 12, 2020 by Faywang - MSFT

User contributions licensed under CC BY-SA 3.0