Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Article

Retrieve detailed information of a File

Rate me:
Please Sign up or sign in to vote.
4.55/5 (19 votes)
14 Aug 2004 285.4K   6.8K   53   30
The article shows how to get the detailed information of a file like comments, author...

Sample Image - DetailFileInfo.jpg

Introduction

This article shows, how one omitted the detail information of a file under the use of the Shell32.dll from the Windows system. Some detailed information are: Comment, Title, Subject, Category, Author, and so on.

Background

The Windows system could store additional information like author, comment, title, category... to a file, this information can be read with GetDetailsOf method from the shell32.dll.

Using the code

First of all, add a new COM reference "Microsoft Shell Controls and Automation" (shell32.dll) to your Project.

After adding the reference, simple create an instance of the CFileInfo class as in the demo project. To access the File Information, simply read the properties from the class.

C#
try{
  // Read File Details from CFileInfo Object
  CFileInfo oDetailedFileInfo = new CFileInfo(sFileName);
  txtName.Text = oDetailedFileInfo.FileName;
  txtFiletype.Text = oDetailedFileInfo.FileType;
  txtFileSize.Text = oDetailedFileInfo.FileSize.ToString();
  txtAuthor.Text = oDetailedFileInfo.FileAuthor;
  txtCategory.Text = oDetailedFileInfo.FileCategory;
  txtComment.Text = oDetailedFileInfo.FileComment;
  txtSubject.Text = oDetailedFileInfo.FileSubject;
  txtTitle.Text = oDetailedFileInfo.FileTitle;
}catch(Exception ex){
  MessageBox.Show("Could not read File information\r\n" 
                  + ex.Message, "Error while getting Info", 
                  MessageBoxButtons.OK,MessageBoxIcon.Error);
}

Behind the Scenes

The GetDetailedFileInfo method gets all available information of a file into an ArrayList. The DetailedFileInfo class is a helper class for holding the information in a simple object within the ArrayList.

C#
// Getting all the available Information of a File into a Arraylist
private ArrayList GetDetailedFileInfo(string sFile){
  ArrayList aReturn = new ArrayList();
  if(sFile.Length>0){
    try{
      // Creating a ShellClass Object from the Shell32
      ShellClass sh = new ShellClass();
      // Creating a Folder Object from Folder that inculdes the File
      Folder dir = sh.NameSpace( Path.GetDirectoryName( sFile ) );
      // Creating a new FolderItem from Folder that includes the File
      FolderItem item = dir.ParseName( Path.GetFileName( sFile ) );
      // loop throw the Folder Items
      for( int i = 0; i < 30; i++ ) {
        // read the current detail Info from the FolderItem Object
        //(Retrieves details about an item in a folder. 
        //For example, its size, type, or the time 
        //of its last modification.)

        // some examples:
        // 0 Retrieves the name of the item. 
        // 1 Retrieves the size of the item.
        // 2 Retrieves the type of the item.
        // 3 Retrieves the date and time that the item was last modified.
        // 4 Retrieves the attributes of the item.
        // -1 Retrieves the info tip information for the item. 

        string det = dir.GetDetailsOf( item, i );
        // Create a helper Object for holding the current Information
        // an put it into a ArrayList
        DetailedFileInfo oFileInfo = new DetailedFileInfo(i,det);
        aReturn.Add(oFileInfo);
      }

    }catch(Exception){

    }
  }
  return aReturn;
}
...

// Helper Class from holding the detailed File Informations
// of the System
public class DetailedFileInfo{
  int iID = 0;
  string sValue ="";

  public int ID{
    get{return iID;}
    set{iID=value;}
  }
  public string Value{
    get{return sValue;}
    set{sValue = value;}
  }

  public DetailedFileInfo(int ID, string Value){
    iID = ID;
    sValue = Value;
  }
}

History

  • First release - 15th August 2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionKeywords? Pin
Member 1485196917-Jun-20 22:05
Member 1485196917-Jun-20 22:05 
QuestionRetrieve detailed information of a File Pin
Dhananjai Verma28-Dec-12 0:47
Dhananjai Verma28-Dec-12 0:47 
GeneralDate Created in the Summary Section Pin
Abdul Rahman El Habboub21-Feb-11 23:14
Abdul Rahman El Habboub21-Feb-11 23:14 
GeneralNot Working in Vista 64 Pin
Tanmoy Mitra9-Dec-10 1:27
Tanmoy Mitra9-Dec-10 1:27 
QuestionCan any 1 tell me how can i found Keyword Information Pin
SheetalJain14-Mar-10 0:10
SheetalJain14-Mar-10 0:10 
QuestionGet Summary Pin
Jackielu200427-Aug-08 21:04
Jackielu200427-Aug-08 21:04 
QuestionNo Keyword? Pin
Member 99144829-May-08 7:56
Member 99144829-May-08 7:56 
AnswerRe: No Keyword? Pin
Member 1485196917-Jun-20 22:06
Member 1485196917-Jun-20 22:06 
GeneralVISTA Support Pin
almkristl27-Aug-07 11:40
almkristl27-Aug-07 11:40 
GeneralRe: VISTA Support Pin
Jason Janofsky4-Dec-07 7:02
Jason Janofsky4-Dec-07 7:02 
GeneralMapped Drive Problem Pin
dwarf888-Aug-07 16:52
dwarf888-Aug-07 16:52 
GeneralRevision Number Pin
Astorya15-Feb-06 1:36
Astorya15-Feb-06 1:36 
QuestionLastSavedBy name Pin
Kavitha Sathishkumar20-Dec-05 23:13
Kavitha Sathishkumar20-Dec-05 23:13 
Generalshell32.dll Pin
Member 10542426-Oct-05 9:28
Member 10542426-Oct-05 9:28 
QuestionRe: shell32.dll Pin
rreeder28-Feb-07 2:38
rreeder28-Feb-07 2:38 
AnswerRe: shell32.dll Pin
Member 1485196918-Jun-20 22:43
Member 1485196918-Jun-20 22:43 
GeneralReading info from CD Pin
Semt-x7-Apr-05 9:36
Semt-x7-Apr-05 9:36 
GeneralWriting This Information Pin
FinallyInSeattle6-Apr-05 8:19
FinallyInSeattle6-Apr-05 8:19 
GeneralWindows 2000 Pin
Member 29993127-Feb-05 23:54
Member 29993127-Feb-05 23:54 
GeneralRe: Windows 2000 Pin
cuepack22-Mar-05 21:21
cuepack22-Mar-05 21:21 
GeneralRe: Windows 2000 Pin
Member 29993123-Mar-05 5:25
Member 29993123-Mar-05 5:25 
QuestionHow to WRITE this data? Pin
John Hind22-Oct-04 8:09
John Hind22-Oct-04 8:09 
AnswerRe: How to WRITE this data? Pin
Anonymous4-Mar-05 4:45
Anonymous4-Mar-05 4:45 
AnswerRe: How to WRITE this data? Pin
Anonymous1-Sep-05 22:08
Anonymous1-Sep-05 22:08 
AnswerRe: How to WRITE this data? Pin
~Dim~27-Mar-07 6:03
~Dim~27-Mar-07 6:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.