如何检测 OneDrive 仅在线文件
How to detect OneDrive online-only files
从 Windows 10 Fall Creators Update(版本 16299.15)和 OneDrive build 17.3.7064.1005 开始,用户可以使用点播文件 (https://support.office.com/en-us/article/learn-about-onedrive-files-on-demand-0e6860d3-d9f3-4971-b321-7092438fb38e)
任何 OneDrive 文件现在都可以具有以下类型之一:仅在线、本地可用和始终可用。
使用 WinAPI 我怎么知道文件(例如 "C:\Users\Username\OneDrive\Getting started with OneDrive.pdf")是在线文件?
查看文件的 PKEY_FilePlaceholderStatus
属性(at the shell level, not the file-system level). This blog post has a example program you can test. 还提示您可能需要查看一些未记录的属性。
Microsoft 有一个 UWP example on MSDN。
要检查 "online only",您只需调用 GetFileAttributes()
并查看是否设置了 FILE_ATTRIBUTE_OFFLINE
属性。
事实上,这对于 OneDrive 来说并不新鲜,该属性已经存在很长时间了。
通过 shell 可以使用其他 OneDrive 属性(尽管您需要的 属性 是 PKEY_StorageProviderState
而不是 PKEY_FilePlaceholderStatus
),但是 "online only" 很简单检查。
编辑:另一个文件系统属性,FILE_ATTRIBUTE_PINNED
是 Windows 10 的新属性,OneDrive 使用它来指示文件是 "always available".
编辑:截至 2019 年,OneDrive 现在似乎使用 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
而不是 FILE_ATTRIBUTE_OFFLINE
,如下所示。
编辑:PKEY_StorageProviderState 在 1903 年 Windows 10 年被破坏,在 1909 年仍未修复。它 returns 4 ("uploading") 用于任何应用程序中的所有文件除了资源管理器。
多年后,我仍在使用 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
描述的属性 here 来确定文件或目录是否完全存在于本地。
Microsoft 文档针对 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
说明如下:
When this attribute is set, it means that the file or directory is not fully present locally. For a file that means that not all of its data is on local storage (e.g. it may be sparse with some data still in remote storage). For a directory it means that some of the directory contents are being virtualized from another location. Reading the file / enumerating the directory will be more expensive than normal, e.g. it will cause at least some of the file/directory content to be fetched from a remote store. Only kernel-mode callers can set this bit.
FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
有一些优点:
- 它可以用于文件和目录。
- 它只能在内核模式下设置,因此任何人都没有机会任意设置该属性。
正如 this answer 中所述,还有一些有趣的未记录属性可以提供有关云文件的附加信息。
注意:我没有接受 Jonathan Potter 的回答,因为我在评论中提到了 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS 属性并且在他更新他的答案之前一年就开始使用它。
从 Windows 10 Fall Creators Update(版本 16299.15)和 OneDrive build 17.3.7064.1005 开始,用户可以使用点播文件 (https://support.office.com/en-us/article/learn-about-onedrive-files-on-demand-0e6860d3-d9f3-4971-b321-7092438fb38e)
任何 OneDrive 文件现在都可以具有以下类型之一:仅在线、本地可用和始终可用。
使用 WinAPI 我怎么知道文件(例如 "C:\Users\Username\OneDrive\Getting started with OneDrive.pdf")是在线文件?
查看文件的 PKEY_FilePlaceholderStatus
属性(at the shell level, not the file-system level). This blog post has a example program you can test.
Microsoft 有一个 UWP example on MSDN。
要检查 "online only",您只需调用 GetFileAttributes()
并查看是否设置了 FILE_ATTRIBUTE_OFFLINE
属性。
事实上,这对于 OneDrive 来说并不新鲜,该属性已经存在很长时间了。
通过 shell 可以使用其他 OneDrive 属性(尽管您需要的 属性 是 PKEY_StorageProviderState
而不是 PKEY_FilePlaceholderStatus
),但是 "online only" 很简单检查。
编辑:另一个文件系统属性,FILE_ATTRIBUTE_PINNED
是 Windows 10 的新属性,OneDrive 使用它来指示文件是 "always available".
编辑:截至 2019 年,OneDrive 现在似乎使用 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
而不是 FILE_ATTRIBUTE_OFFLINE
,如下所示。
编辑:PKEY_StorageProviderState 在 1903 年 Windows 10 年被破坏,在 1909 年仍未修复。它 returns 4 ("uploading") 用于任何应用程序中的所有文件除了资源管理器。
多年后,我仍在使用 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
描述的属性 here 来确定文件或目录是否完全存在于本地。
Microsoft 文档针对 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
说明如下:
When this attribute is set, it means that the file or directory is not fully present locally. For a file that means that not all of its data is on local storage (e.g. it may be sparse with some data still in remote storage). For a directory it means that some of the directory contents are being virtualized from another location. Reading the file / enumerating the directory will be more expensive than normal, e.g. it will cause at least some of the file/directory content to be fetched from a remote store. Only kernel-mode callers can set this bit.
FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
有一些优点:
- 它可以用于文件和目录。
- 它只能在内核模式下设置,因此任何人都没有机会任意设置该属性。
正如 this answer 中所述,还有一些有趣的未记录属性可以提供有关云文件的附加信息。
注意:我没有接受 Jonathan Potter 的回答,因为我在评论中提到了 FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS 属性并且在他更新他的答案之前一年就开始使用它。