Search code examples
c#onedrive

How to get Files status from OneDrive in c# without using any APIs?


I am developing Windows application to identify if this file cloud or local from OneDrive using c#.

OneDrive has three types file status like (Cloud, offline, always available on this device) from code how do I get file status of each file in OneDrive.

From what i have searched,API need user's account to log in Azure or something,can we not using API?

Please suggest.

Regards.


Solution

  • Yes, you can detect a file that is not downloaded to a local OneDrive folder by examining the file's attributes. OneDrive uses a flag to indicate that the file is just a placeholder. Unfortunately the particular flag is not one of those enumerated in FileAttributes so you'll have to test it separately.

    A quick and dirty test for a OneDrive placeholder file looks something like this:

    static bool IsPlaceholder(string filename)
    {
        FileInfo info = new(filename);
        return info.Exists && (((int)info.Attributes) & 0x00400000) != 0;
    }
    

    The longer version is that there are a few NTFS file attributes that dotnet doesn't support. From this list you'll find that the one we're interested in is called FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS (0x00400000). This is generally set only by kernel-level drivers, such as the one OneDrive uses to download file content on demand.