【问题标题】:How to open OneDrive files from WPF application如何从 WPF 应用程序打开 OneDrive 文件
【发布时间】:2014-11-06 03:16:03
【问题描述】:

当我尝试从 WPF 应用程序打开 OneDrive 文件路径时,使用 File.ReadAllText(filename); 时出现以下错误:

The file cannot be accessed by the system.

尽管尝试显式检查读取权限:

private static bool HasReadPermissions(string filename)
{
    FileSystemSecurity security = File.GetAccessControl(filename);
    var rules = security.GetAccessRules(true, true, typeof(NTAccount));
    var currentUser = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    foreach (FileSystemAccessRule rule in rules)
    {
        if (!currentUser.IsInRole(rule.IdentityReference.Value) || (rule.FileSystemRights & (FileSystemRights.ReadData | FileSystemRights.Read)) == 0)
        {
            continue;
        }
        if (rule.AccessControlType == AccessControlType.Deny)
        {
            return false;
        }
        if (rule.AccessControlType == AccessControlType.Allow)
        {
            return true;
        }
    }
    return false;
}

是否有任何方法可以检查 OneDrive 权限或检测文件是否为 OneDrive 文件或在 WPF 应用程序中实际打开 OneDrive 文件?我认为这个文件只能在线获得这一事实可能是问题所在;有没有办法检测 WPF 或 Windows 桌面应用程序中的仅在线文件?

【问题讨论】:

标签: c# .net wpf file-io onedrive


【解决方案1】:

这个问题比较老,所以这个答案主要是给通过搜索找到它的读者。

您可以在桌面应用程序中判断文件是否为仅在线 OneDrive 文件。只需检查文件属性,如果附加了脱机属性,该文件将是仅限联机的 OneDrive 文件。以下是示例方法:

public static bool IsFileAvailable(string filePath)
{
    if (!File.Exists(filePath))
        return false;

    var attributes = File.GetAttributes(filePath);

    Debug.WriteLine(attributes);
    // Available online-only: Hidden, System, Archive, SparseFile, ReparsePoint, Offline
    // Available offline: Archive

    return !attributes.HasFlag(FileAttributes.Offline);
}

离线属性并不直接表示该文件是仅在线OneDrive文件,但该文件的数据不是立即可用的,我想这是你想知道的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2021-10-27
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多