读取文件/文件夹可用性状态

时间:2018-08-30 07:53:49

标签: c# system.io.fileinfo

C#-我正在构建一个控制台应用程序以读取OneDrive文件夹状态。 我可以读取文件/文件夹的许多属性,但不知道如何在此处获取“状态”列的值。

已更新:此方法与此处描述的方法不同(使用Shell32,注册表...) How can I check local OneDrive folder is in sync?

所以它不是重复的问题

Status property

enter image description here

1 个答案:

答案 0 :(得分:0)

添加对Shell32的引用,例如C:\ Windows \ SysWOW64 \ shell32.dll (我确实尝试过WindowsAPICodePack ShellPropertyCollection,但这没用)

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; }
}