Raf's laboratory Abstracts Feed Raffaele Rialdi personal website

I am Raf logo

Read shell file properties

October 21, 2009
http://www.iamraf.net/Samples/Read-shell-file-properties

Some months ago, I talked with Diego Cattaruzza about the chance to read the file properties.

It happens that "depends" on the file type. A couple of years ago, if I remember well, I used some PInvoke code to read file properties via COM Structured Storage.

In these days OPC files have properties inside their package in XML format. But if you need to read or write the properties of a generic file you have to deal with the Shell directly.

The first step is to add a reference to the Shell COM object called "Microsoft Shell Controls And Automation".

Now we can read all the file properties using the following snippet:

   1: private static void PrintAllShellProps(string Filename)
   2: {
   3:     FileInfo file = new FileInfo(Filename);
   4:  
   5:     Shell32.ShellClass shell = new Shell32.ShellClass();
   6:     Shell32.Folder folder = shell.NameSpace(file.DirectoryName);
   7:  
   8:     Shell32.FolderItem item = null;
   9:     foreach(Shell32.FolderItem i in folder.Items())
  10:     {
  11:         if(i.Name == file.Name)
  12:         {
  13:             item = i;
  14:             break;
  15:         }
  16:     }
  17:  
  18:     if(item == null)
  19:         return;
  20:  
  21:  
  22:     for(int i = 0; i < 34; i++)
  23:     {
  24:         var columns = folder.GetDetailsOf(file.Name, i);
  25:         var values = folder.GetDetailsOf(item, i);
  26:         string info = string.Format("{0}\t{1}:\t{2}", i, columns, values);
  27:         Console.WriteLine(info);
  28:     }
  29: }


rated by 0 users



Share this page on Twitter


Privacy | Legal Copyright © Raffaele Rialdi 2009, Senior Software Developer, Consultant, p.iva IT01741850992, hosted by Vevy Europe Advanced Technologies Division. Site created by Raffaele Rialdi, 2009 - 2015 Hosted by: © 2008-2015 Vevy Europe S.p.A. - via Semeria, 16A - 16131 Genova - Italia - P.IVA 00269300109