C# 向文件添加新的元数据属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19947887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add new metadata properties to a file
提问by user2399378
I want to add some metadata properties to some files. Just like there are Owner, Computer, Title, Subject, etc for doc files, I want to be able to add some custom attributes. How can that be done?
我想向某些文件添加一些元数据属性。就像文档文件有所有者、计算机、标题、主题等一样,我希望能够添加一些自定义属性。那怎么办呢?
采纳答案by jAC
As already mentioned it depends on the filesystem. So this will only work with NTFS.
如前所述,它取决于文件系统。所以这只适用于 NTFS。
One way is creating ADS streams: See the edit history.
一种方法是创建 ADS 流:查看编辑历史记录。
Another way is using the DSOFile-Library, which is intended to work on Office files only. But it works on every file.
另一种方法是使用 DSOFile-Library,它仅用于处理 Office 文件。但它适用于每个文件。
First of all download the library here (x64+x86): DOWNLOAD
首先在这里下载库(x64+x86):下载
IMPORTANT:
Since DSO OLE is 32bit DLL it will only work, when you set your compilation target CPU to x86. Otherwise it will throw an Exception.There's also a 64bit version avaliable: How to read custom file properties in c#
重要提示:
由于 DSO OLE 是 32 位 DLL,因此它仅在您将编译目标 CPU 设置为 x86 时才有效。否则会抛出异常。还有一个 64 位版本可用:How to read custom file properties in c#
Then create a refernce to the COM DLL in your project (Right click on the solution -> Add reference -> COM tab -> Add "DSO OLE Document Property Reader v2.1") and use the namespace:
然后在您的项目中创建对 COM DLL 的引用(右键单击解决方案 -> 添加引用 -> COM 选项卡 -> 添加“DSO OLE 文档属性阅读器 v2.1”)并使用命名空间:
using DSOFile;
After that you can create your own attributes:
之后,您可以创建自己的属性:
First of all open the file:
首先打开文件:
OleDocumentProperties myFile = new DSOFile.OleDocumentProperties();
myFile.Open(@"MYPATHHERE", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
Create a object for yourValue: object yourValue = "Your Value";
为 yourValue 创建一个对象: object yourValue = "Your Value";
Then check if there's already a property like the one you want to create:
然后检查是否已经有一个类似于您要创建的属性:
foreach (DSOFile.CustomProperty property in myFile.CustomProperties)
{
if (property.Name == "Your Property Name"){
//Property exists
//End the task here (return;) oder edit the property
property.set_Value(yourValue);
}
}
Then after checking for existing attributes, you can add the attribute:
然后在检查现有属性后,您可以添加属性:
myFile.CustomProperties.Add("Your Property Name", ref yourValue);
To finish the task, save and close the file:
要完成任务,请保存并关闭文件:
myFile.Save();
myFile.Close(true);
You can download a sample project on my homepage.
Now to the part of showing the attributes in the explorer.
现在到在资源管理器中显示属性的部分。
You have to create a shell extension for that. For more info on that, visit the Codeproject page.
您必须为此创建一个 shell 扩展。有关更多信息,请访问Codeproject 页面。
I created one, you can download it here.But you have to sign it again (look for a "how-to" on the mentioned page).
我创建了一个,你可以在这里下载。但是您必须再次签名(在提到的页面上查找“操作方法”)。
It will look like that, when right-clicking on a .css/.js/.txt-file:
Or create your own properties tab:
You can download the sample here: DOWNLOAD
当右键单击 .css/.js/.txt 文件时,它看起来像这样:
或者创建您自己的属性选项卡:
您可以在此处下载示例:下载
For more information about Dsofile.dll and other source see Microsoft Dsofile.dll
有关 Dsofile.dll 和其他源的详细信息,请参阅Microsoft Dsofile.dll