15

I need to extract the date from the "Media Created" column (highlighted in green in my the example photo below) using C#.

In my example, the "Media Created" and "Date" columns are the exact same. However, there are several instances where they are not. The "Media Created" column contains the correct date for when the video was actually recorded.

See this column?

Here is the function I used to get it. Thanks to Aziz for pointing me in the right direction:

Shell shell = new ShellClass();
Folder folder = shell.NameSpace(_File.DirectoryName);
FolderItem file = folder.ParseName(_File.Name);

// These are the characters that are not allowing me to parse into a DateTime
char[] charactersToRemove = new char[] {
    (char)8206,
    (char)8207
};

// Getting the "Media Created" label (don't really need this, but what the heck)
string name = folder.GetDetailsOf(null, 191);

// Getting the "Media Created" value as a string
string value = folder.GetDetailsOf(file, 191).Trim();

// Removing the suspect characters
foreach (char c in charactersToRemove)
    value = value.Replace((c).ToString(), "").Trim();

// If the value string is empty, return DateTime.MinValue, otherwise return the "Media Created" date
return value == string.Empty ? DateTime.MinValue : DateTime.Parse(value);
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Jason Thuli
  • 736
  • 2
  • 8
  • 23
  • 1
    1. 208 instead of 191 on Windows 10. 2. The code should be start in single-threaded apartment (STA). Use [STAThread] attribute or Thread with ApartmentState.STA – MirrorBoy Jul 06 '19 at 12:51
  • If you are using Python, you can try [*Getting metadata for MOV video*](https://stackoverflow.com/a/54683292/3357935). – Stevoisiak Nov 13 '20 at 15:07
  • Thanks for edit, but what's assembly/namespace of ShellClass class pls ? – GGO Oct 19 '21 at 09:13

2 Answers2

5

There are another way to retrieve data, using the Microsoft.WindowsAPICodePack.Shell namespace.

ShellObject shell = ShellObject.FromParsingName(path);

var data = shell.Properties.System.Media.DateEncoded;
Rafael Cronemberger
  • 231
  • 1
  • 4
  • 11
  • 2
    Good point. For Gopro MP4 files, "System.Media.DateEncoded" property returns the date the media was created. – konahn Apr 19 '21 at 06:09
  • 1
    I like this solution as it's short and clean. I only had to install the Nuget package "Microsoft.WindowsAPICodePack-Shell". Then to read the date: ShellObject shell = ShellObject.FromParsingName(filepath); DateTime datetime = shell.Properties.System.Media.DateEncoded.Value.Value; – Val Aug 27 '22 at 17:57
5

The extended file properties can be obtained by using Folder.GetDetailsOf() method. As per this thread, the Media Created Date can be retrieved using a property id of 177.

Aziz
  • 534
  • 5
  • 23
  • That worked perfect, except for a few things. The property id was 191. I'm guessing it has to do with the operating system (I'm on Windows 7 64-bit). There were also some weird characters in there that would not parse into a DateTime correctly: (char)8206 (char)8207 But once they were stripped out it worked perfectly! Thanks so much! – Jason Thuli Dec 02 '11 at 13:32
  • 1
    There is an explanation [here](https://stackoverflow.com/questions/22382010/what-options-are-available-for-shell32-folder-getdetailsof) of how to get the correct ID and avoid the magic numbers. – Robert Ellison Dec 09 '18 at 00:10