C# .Net 3.5 : Extracting and binding Metadata to a file

0

Introduction

I am trying to make a compression application. The current obstacle I am facing is that whenever I try to compress my file I take a byte array from the file and apply compression algorithm on the byte array itself because of which the metadata of file is lost.

Question

Is there any method by which I can extract the metadata of a file on compression and later on extraction attach the metadata to the extracted file?

Visual Studio : VS2008
Framework : .Net 3.5

Solutions I found:

  1. I have seen in many articles that they say we can use Windows Property System but even after reading the article I have no clue as to how can I implement it.
  2. This website has explained with the code but they didn't give any download link for the DLL.
  3. From this Stackoverflow answer I got this code:-
//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();

I get the following error on Line 8

The name is not valid. (Exception from HRESULT: 0x800300FC (STG_E_INVALIDNAME))

c#
.net-3.5
metadata
asked on Stack Overflow Sep 26, 2018 by Agent_Spock • edited Sep 26, 2018 by Collin ter Steege

1 Answer

0

Are you sure that the Company property exists in your file metadata? Try using a known existing property in the metadata of the file you're trying to access, as the ones that are used in the example may just not exist.

As for saving the properties, you can access some basic global ones like CreationTime and LastAccessTime from the System.IO.FileInfo object.

This article seems to describe a method through which you can get more specific properties from files, such as the Camera and CameraManufacturer properties (that isn't identical to the one from the StackOverflow question) like so:

 

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));
    }
}

with GetValue being:

private static string GetValue(IShellProperty value)
{
    if (value == null || value.ValueAsObject == null)
    {
        return String.Empty;
    }
    return value.ValueAsObject.ToString();
}
answered on Stack Overflow Sep 26, 2018 by Larry Tang

User contributions licensed under CC BY-SA 3.0