【问题标题】:C# .Net 3.5 : Extracting and binding Metadata to a fileC# .Net 3.5:提取元数据并将其绑定到文件
【发布时间】:2018-09-26 13:04:26
【问题描述】:

简介

我正在尝试制作一个压缩应用程序。我目前面临的障碍是,每当我尝试压缩文件时,我都会从文件中获取一个字节数组,并对字节数组本身应用压缩算法,因此 文件的元数据会丢失

问题

有什么方法可以在压缩时提取文件的元数据,然后在提取时将元数据附加到提取的文件中?

Visual Studio : VS2008
框架:.Net 3.5

我找到的解决方案:

  1. 我在很多文章中看到他们说我们可以使用Windows Property System,但即使在阅读了这篇文章后,我也不知道如何实现它。
  2. This 网站已经解释了代码,但他们没有给出 DLL 的任何下载链接。
  3. 从这个Stackoverflow answer我得到了这个代码:-
//creates new class of oledocumentproperties
var doc = new OleDocumentPropertiesClass();

//open your selected file
doc.Open(@"C:\Users\ABC\Desktop\Test\1.jpg", false, dsoFileOpenOptions.dsoOptionDefault);

//you can set properties with summaryproperties.nameOfProperty = value; for example
doc.SummaryProperties.Company = "lol"; //Line 8 : Shows error
doc.SummaryProperties.Author = "me";

//after making changes, you need to use this line to save them
doc.Save();

我在 第 8 行

收到以下错误

名称无效。 (来自 HRESULT 的异常:0x800300FC (STG_E_INVALIDNAME))

【问题讨论】:

    标签: c# .net-3.5 metadata


    【解决方案1】:

    您确定Company 属性存在于您的文件元数据中吗? 尝试在您尝试访问的文件的元数据中使用已知的现有属性,因为示例中使用的属性可能不存在。

    至于保存属性,您可以从System.IO.FileInfo 对象访问一些基本的全局属性,例如CreationTimeLastAccessTime

    This article 似乎描述了一种方法,您可以通过该方法从文件中获取更具体的属性,例如 CameraCameraManufacturer 属性(与 StackOverflow 问题中的属性不同),如下所示:

     

    using System;
    using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
    using Microsoft.WindowsAPICodePack.Shell;
    using System.Diagnostics;
    
    class Program {
        void getProperty() {
            var cameraModel = GetValue(picture.Properties.
            GetProperty(SystemProperties.System.Photo.CameraModel));
        }
    }
    

    GetValue 为:

    private static string GetValue(IShellProperty value)
    {
        if (value == null || value.ValueAsObject == null)
        {
            return String.Empty;
        }
        return value.ValueAsObject.ToString();
    }
    

    【讨论】:

    • "WindowsAPICodePack" 适用于 VS2016,我猜是在 GitHub 中编写的......问题中提到的代码告诉了如何“更改元数据”,是的,该属性原样存在由智能感知本身建议。
    猜你喜欢
    • 2013-04-14
    • 2011-01-07
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多