c# - 在 Windows 资源管理器中显示自定义标题或列

标签 c# .net windows shell-extensions

我的应用向文件添加了一些自定义元数据。我想像这样在 Windows 资源管理器中显示它:

Mockup 1

或者这个:

Mockup 2

有没有办法在 .NET 中做到这一点?

最佳答案

在 Windows 文件管理器中构建自定义列有两种方法:使用 Windows 属性系统和云存储提供程序的属性定义。您通常会使用第一种方法为您拥有的文件类型创建自定义属性。在显示来自文档管理系统或任何其他存储的自定义数据时,您将使用第二种方法。

使用 Windows 属性系统。

您可以 create custom properties适用于 Windows Vista 和更高版本中的特定文件类型。这些属性可以是只读的或读写的。它们也可以被 Window Search 索引器索引并参与搜索。有一些限制:

...property handlers cannot be implemented in managed code and should be implemented in C++.

  • 该属性与特定文件类型相关联,通常属于您的应用程序。您不能为所有文件类型创建属性。

使用云存储提供商属性定义

在 Windows 10 Creators Update 及更高版本中,您可以为使用 Cloud Sync Engine API 创建的文件系统添加自定义列(存储提供商、云过滤器 API)。该 API 用于 OneDrive 等工具。您需要使用自定义属性定义注册一个云存储提供程序同步根,为您的自定义列提供数据,最后使用云文件/云过滤器 API 实现一个云存储提供程序。 enter image description here

属性定义与文件类型无关,可以为所有文件添加。此外,即使 .NET 中只有一些 API 可用,您仍然可以调用 Win32 函数并仅使用托管代码构建云提供程序。

注册云存储提供程序。以下是在 C# 中使用自定义列注册存储提供程序的示例:

StorageProviderSyncRootInfo storageInfo = new StorageProviderSyncRootInfo();
storageInfo.Path = await StorageFolder.GetFolderFromPathAsync("C:\\Users\\User1\\VFS\\");
...
        
// Adds columns to Windows File Manager. 
// Show/hide columns in the "More..." context menu on the columns header.
var proDefinitions = storageInfo.StorageProviderItemPropertyDefinitions;
proDefinitions.Add(new StorageProviderItemPropertyDefinition { DisplayNameResource = "Lock Expires", Id = 2, });
proDefinitions.Add(new StorageProviderItemPropertyDefinition { DisplayNameResource = "Lock Scope", Id = 3, });
        
StorageProviderSyncRootManager.Register(storageInfo);

可以找到完整的注册示例here .

为属性定义提供数据。要为列提供数据,您将使用 StorageProviderItemProperties.SetAsync() 调用:

IStorageItem storageItem = await Windows.Storage.StorageFile.GetFileFromPathAsync(path);
StorageProviderItemProperty propState = new StorageProviderItemProperty()
{
     Id = 3,
     Value = "Exclusive",
     IconResource = "C:\\path\\icon.ico" // The optional icon to be displayed in the Status column.
};
await StorageProviderItemProperties.SetAsync(storageItem, new StorageProviderItemProperty[] { propState });

另一种方法是实现 IStorageProviderItemPropertySource界面。它根据您的文件路径返回属性。

云存储供应商实现。最后,您需要一个完整的文件系统实现,为您的文件/文件夹占位符提供数据。您可以在 .NET/C# here 中找到完整的示例:

关于c# - 在 Windows 资源管理器中显示自定义标题或列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648275/

相关文章:

c# - 我如何知道应用程序池是否已远程启动?

c++ - 查找 DLL 的 CLR 版本

c# - Wpf 渐变等效于 css 渐变

c# - 使用 .NET 将 Json 转换为 List<object>

php - COM 对象方法未定义

python - Pip 安装到 anaconda 目录而不是 python 目录 (Windows)

c# - Azure函数: Have a CorrelationId between Http Trigger and Blob Trigger to follow a request

c# - 在不安装 Excel 的情况下运行 Excel 宏

c# - 在 WPF 中打印带有动态数据的流文档

windows - 什么会导致 C++Builder/Delphi 线程和应用程序无法关闭?