Capturing File Information


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.

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.

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:

private void btnBrowse_Click(object sender, EventArgs e)


    openFileDialog1.InitialDirectory = "c:\\";

    openFileDialog1.Filter = "All files (*.*)|*.*";

    openFileDialog1.Title = "Select File";

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)




            txtFilePath.Text = openFileDialog1.FileName.ToString();



        catch (Exception ex)


            MessageBox.Show("Cannot read file from disk: " +





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:

private void ShowFileInfo(string sFilePath)


    // Part 1:  File Version Information

    System.Diagnostics.FileVersionInfo fileVersInfo =


    lblCompanyName.Text = "Company Name:  " + fileVersInfo.CompanyName;

    lblFileName.Text = "File Name:  " + fileVersInfo.FileName;

    lblProductName.Text = "Product Name:  " + fileVersInfo.ProductName;

    lblVersion.Text = "Version:  " + fileVersInfo.FileVersion;

    lblComments.Text = "Comments:  " + fileVersInfo.Comments;

    lblIsPatched.Text = "Is Patched:  " + fileVersInfo.IsPatched;

    lblCopyright.Text = "Copyright:  " + fileVersInfo.LegalCopyright;

    lblTrademark.Text = "Trademark:  " + fileVersInfo.LegalTrademarks;

    lblDescription.Text = "Description: " + fileVersInfo.FileDescription;

    lblInternalName.Text = "Internal Name:  " + fileVersInfo.InternalName;


    // Part 2:  File Information

    System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath);

    lblAttributes.Text = "Attributes:  " + fi.Attributes.ToString();

    lblCreationDate.Text = "Creation Date:  " +


    lblCreationTime.Text = "Creation Time:  " +


    lblLastAccessDate.Text = "Last Access Date: " +


    lblLastAccessTime.Text = "Last Access Time:  " +


    lblLastWriteDate.Text = "Last Write Date:" +


    lblLastWriteTime.Text = "Last Write Time:" +


    lblFileSize.Text = "File Size:  " + fi.Length.ToString();

    lblIsReadOnly.Text = "Read Only:  " + fi.IsReadOnly.ToString();


    // these values can all be changed

    //fi.LastAccessTime = DateTime.Now;

    //fi.CreationTime = DateTime.Now;

    //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 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.


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