Retreived properties for StorageFile are empty in some cases

Peter Bons 20 Reputation points
2023-04-18T11:19:34.2433333+00:00

I am reading some properties of a given file in order to know its location on One Drive. For that I use the following code:

var file = await StorageFile.GetFileFromPathAsync(path);

            var props = await file?.Properties.RetrievePropertiesAsync(new[]
            {
                "System.FolderNameDisplay",
                "System.StorageProviderFileRemoteUri",
                "System.StorageProviderId"
            });

On most machines this yields a result when tested against a cloud service sync file (One Drive). The StorageProviderFileRemoteUri is an http url pointing to the online location. On some machines however the result is an empty string. Does anyone have any idea what might cause this. Things I did:

  • Make sure the windows version is supported
  • Target framework is set to net6.0-windows10.0.19041.0
  • The path is actually a local one drive path
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,455 questions
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
860 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,370 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 35,716 Reputation points Microsoft Vendor
    2023-04-21T13:17:27.09+00:00

    Hi @Peter Bons ,

    Update: Upgrading the system from 10.0.22000.0 to 10.0.22621.0 fixed the issue.


    The error you said did not appear. But after I added the microsoft.windows.sdk.net.ref in your file, I can use StorageFile. If you want to use a similar approach in winform .net 6.0 to the project you have already created. You can take a look at my code below: I start it with a button and type the output into a richtextbox.

    private async void button1_Click(object sender, EventArgs e)
    {
        var path = @"C:\Users\Administrator\OneDrive\OneDrive.pdf"; 
        if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
        {
            richTextBox1.AppendText("Provide a valid path pointing to a file on OneDrive folder\nFor Example: C:\\Users\\pbons\\OneDrive - Brink\test.txt\n");
            return;
        }
    
        var file = await StorageFile.GetFileFromPathAsync(path);
    
        var props = await file?.Properties.RetrievePropertiesAsync(new[]
        {
            "System.FolderNameDisplay",
            "System.StorageProviderFileRemoteUri",
            "System.StorageProviderId"
        });
    
        foreach (var prop in props)
            richTextBox1.AppendText($"{prop.Key}: {prop.Value}\n");
    
        richTextBox1.AppendText($"DotNet version: {Environment.Version}\n");
        richTextBox1.AppendText($"OS version: {Environment.OSVersion}\n");
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful