Capturing File Information

Introduction


This article describes a simple approach to capturing and displaying file and file version information. Figure 1 of this document demonstrates some of the information that may be captured through the use of the System.IO.FileInfo and System.Diagnostics.FileVersionInfo classes.

Displaying File Related Information
Figure 1. Displaying File Related Information

Getting Started


There is a single Windows Forms application included with this download. You may open the project and examine the project if you wish; however, the code is quite simple and will be described in the following sections of this document. All examples were written using Visual Studio 2005 using C#; the same code could also be used with earlier versions of Visual Studio. The project does not use any references aside from the defaults, there is only a single main form and program file included in the solution.

The Project in the IDE's Solution Explorer
Figure 2. The Project in the IDE's Solution Explorer

Code: The Main Form


The application contains a single form; the form contains a file selection group box that contains a text box used to display the path to the selected file and a browse button which is used to open an Open File Dialog box; file selections made in the Open File Dialog are shown in the file path text box and the file path is also used to point to the file to be examined.

Aside from the file selection group box and its controls, there is a second group box labeled, "View Details"; this group box contains a set of labels used to display information about the selected file; such information is gathered using the System.File.IO and System.Diagnostics.FileVersionInfo classes. Whenever the user selects a file, the file is passed to a function entitled, "ShowFileInfo" which populates each of the labels with the information appropriate to the file selected.

The click event handler for the button used to browse for and select a file is as follows,

  1. private void btnBrowse_Click(object sender, EventArgs e)  
  2. {  
  3.     openFileDialog1.InitialDirectory = "c:\\";  
  4.     openFileDialog1.Filter = "All files (*.*)|*.*";  
  5.     openFileDialog1.Title = "Select File";  
  6.     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
  7.     {  
  8.         try  
  9.         {  
  10.             txtFilePath.Text = openFileDialog1.FileName.ToString();  
  11.             ShowFileInfo(txtFilePath.Text.Trim());  
  12.         }  
  13.         catch (Exception ex)  
  14.         {  
  15.             MessageBox.Show("Cannot read file from disk: " +  
  16.             ex.Message.ToString());  
  17.         }  
  18.     }  

The button click event sets the initial directory and filter for the open file dialog box, the title of the dialog box is set to display "Select File" and the initial directory is set to point to the C root. The filter is set to allow the user to view and select files with any extension.

If the user does not cancel the open file dialog, whenever a file is selected, the text box is set to display the full path to the selected file along with the file name itself. The file path, once stored in the text box, is then passed to a function entitled "ShowFileInfo"; this function in turns captures the file information and sends it to the form for display to the user.

The "ShowFileInfo" function contains the following code,

  1. private void ShowFileInfo(string sFilePath)  
  2. {  
  3.     // Part 1:  File Version Information  
  4.     System.Diagnostics.FileVersionInfo fileVersInfo =  
  5.     System.Diagnostics.FileVersionInfo.GetVersionInfo(sFilePath);  
  6.     lblCompanyName.Text = "Company Name:  " + fileVersInfo.CompanyName;  
  7.     lblFileName.Text = "File Name:  " + fileVersInfo.FileName;  
  8.     lblProductName.Text = "Product Name:  " + fileVersInfo.ProductName;  
  9.     lblVersion.Text = "Version:  " + fileVersInfo.FileVersion;  
  10.     lblComments.Text = "Comments:  " + fileVersInfo.Comments;  
  11.     lblIsPatched.Text = "Is Patched:  " + fileVersInfo.IsPatched;  
  12.     lblCopyright.Text = "Copyright:  " + fileVersInfo.LegalCopyright;  
  13.     lblTrademark.Text = "Trademark:  " + fileVersInfo.LegalTrademarks;  
  14.     lblDescription.Text = "Description: " + fileVersInfo.FileDescription;  
  15.     lblInternalName.Text = "Internal Name:  " + fileVersInfo.InternalName;  
  16.     // Part 2:  File Information  
  17.     System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath);  
  18.     lblAttributes.Text = "Attributes:  " + fi.Attributes.ToString();  
  19.     lblCreationDate.Text = "Creation Date:  " +  
  20.     fi.CreationTime.ToLongDateString();  
  21.     lblCreationTime.Text = "Creation Time:  " +  
  22.     fi.CreationTime.ToLongTimeString();  
  23.     lblLastAccessDate.Text = "Last Access Date: " +  
  24.     fi.LastAccessTime.ToLongDateString();  
  25.     lblLastAccessTime.Text = "Last Access Time:  " +  
  26.     fi.LastAccessTime.ToLongTimeString();  
  27.     lblLastWriteDate.Text = "Last Write Date:" +  
  28.     fi.LastWriteTime.ToLongDateString();  
  29.     lblLastWriteTime.Text = "Last Write Time:" +  
  30.     fi.LastWriteTime.ToLongTimeString();  
  31.     lblFileSize.Text = "File Size:  " + fi.Length.ToString();  
  32.     lblIsReadOnly.Text = "Read Only:  " + fi.IsReadOnly.ToString();  
  33.     // these values can all be changed  
  34.     //fi.LastAccessTime = DateTime.Now;  
  35.     //fi.CreationTime = DateTime.Now;  
  36.     //fi.LastWriteTime = DateTime.Now;  

As advertised, the path to the selected is accepted as argument by the function. The file path is used to create an instance of the FileVersionInfo class which is populated with the file version information associated with the selected file. From there, the labels used to display the file version information are populated. Next, an instance of the FileInfo class is created and set to capture the file information from the selected file. From there, the labels associated with the file information are populated.

There are three commented out lines at the end of this function; these were included to demonstrate that it is possible to overwrite some of the file information through the FileInfo class. In the commented out lines, the last access time, creation time, and last write time values are all overwritten with the current date and time.

To test this portion of the function, you may uncomment the three lines of code and run the application; once you select a file and its information is displayed, the three time and date related values will be overwritten with the current date and time; if you then reopen the file, you will not that all of these values have been updated accordingly.

Summary


This article demonstrates a couple of different ways to capture some information from a file; further, the article indicates a process through which file information may be changed.

 


Similar Articles