1

C# - I'm building a console app to read the OneDrive folder status. I can read so many attributes of file/folder but don't know how to get the Status column value here.

UPDATED: This approach is different that the one that describe here (using Shell32, registry...) How can I check local OneDrive folder is in sync?

So it is not duplicated question

Status property

enter image description here

ndh103
  • 96
  • 1
  • 6
  • @Adriani6 not duplicated, here is another approach instead of reading icon from Shell32 – ndh103 Aug 30 '18 at 08:46
  • It is a duplicate, it's doing exactly what you're attempting to do. Also in that case your question will get closed for other reasons such as failing to show your current code with a specific part failing (your problem) for someone to help you. You also didn't specify exactly what your "approach" is, all you said is that you're building an application and you can read some properties and you added 2 screenshots... this is way too broad. – Adrian Aug 30 '18 at 09:02
  • @Adriani6 sorry about that, for some security reason, I cannot follow the approach of reading Icon from file/folder to determine the status. My approach is to find a way to read the file/folder attribute: Status , since I can see it in the property dialog of window – ndh103 Aug 30 '18 at 09:06
  • Did you take a look at https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-determining-availability-of-microsoft-onedrive-files ? – Adrian Aug 30 '18 at 09:21
  • Unfortunately, does not work in my case – ndh103 Sep 14 '18 at 09:03

1 Answers1

1

Add reference to Shell32 e.g C:\Windows\SysWOW64\shell32.dll ( I did try the WindowsAPICodePack ShellPropertyCollection but that did not work )

public static class OneDriveExtensions
{
    private static int GetAvailabilityStatusIndex(Folder folder)
    {
        var index = 0;
        while (true)
        {
            var details = folder.GetDetailsOf(folder, index);
            if (details == "Availability status")
            {
                return index;
            }
            index++;
        }
    }
    public static string OneDriveAvailability(this FileInfo file)
    {
        int availabilityStatusIndex;
        return OneDriveAvailability(file, out availabilityStatusIndex);
    }
    public static string OneDriveAvailability(this FileInfo file,out int availabilityStatusIndex)
    {

        Shell shell = new Shell();
        Folder objFolder = shell.NameSpace(file.DirectoryName);
        availabilityStatusIndex = GetAvailabilityStatusIndex(objFolder);
        return objFolder.GetDetailsOf(objFolder.ParseName(file.Name), availabilityStatusIndex);

    }
    public static string OneDriveAvailability(this FileInfo file, int availabilityStatusIndex)
    {
        Shell shell = new Shell();
        Folder objFolder = shell.NameSpace(file.DirectoryName);
        FolderItem objFolderItem = objFolder.ParseName(file.Name);
        return objFolder.GetDetailsOf(objFolderItem, availabilityStatusIndex);

    }
    public static IEnumerable<OneDriveFileInfo> OneDriveAvailability(this DirectoryInfo directory,Func<DirectoryInfo,IEnumerable<FileInfo>> files)
    {
        var requireIndex = true;
        int availabilityStatusIndex = 0;
        return files(directory).Select(f =>
        {
            string oneDriveAvailability;
            if (requireIndex)
            {
                requireIndex = false;
                oneDriveAvailability= f.OneDriveAvailability(out availabilityStatusIndex);
            }
            else
            {
                oneDriveAvailability= f.OneDriveAvailability(availabilityStatusIndex);
            }
            return new OneDriveFileInfo(oneDriveAvailability, f);
        });
    }
    public static IEnumerable<OneDriveFileInfo> OneDriveAvailability(this IEnumerable<FileInfo> files,int availabilityIndex)
    {
        return files.Select(f => new OneDriveFileInfo(f.OneDriveAvailability(availabilityIndex), f));
    }
}
public class OneDriveFileInfo
{
    public OneDriveFileInfo(string availabilityStatus, FileInfo file)
    {
        this.AvailabilityStatus = availabilityStatus;
        this.File = file;
    }
    public string AvailabilityStatus { get; private set; }
    public FileInfo File { get; private set; }
}
user487779
  • 550
  • 5
  • 12
  • This would be perfect if it always got the details/properties in English, but it doesn't. So on a danish PC, it's stuck in the while-loop forever, as it never finds "Availability status". – Albert MN. Jul 14 '19 at 15:27
  • @Albert mn. A culture dictionary lookup then, once you know what the term is in danish – user487779 Jul 16 '19 at 16:51
  • is that a Windows feature? Something that can be relied on, not just a translator that might translate it wrong? – Albert MN. Jul 17 '19 at 20:02
  • You would need to know what the term would be in the language that you are interested in. If you use file / details on a Danish pc is there an 'avalability status' entry that can be used in the code I provided ? For a general solution perhaps it is possible to use win32 API to switch the language and check the term to create the language lookup. – user487779 Jul 18 '19 at 21:29