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.
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)
{
try
{
txtFilePath.Text = openFileDialog1.FileName.ToString();
ShowFileInfo(txtFilePath.Text.Trim());
}
catch (Exception ex)
{
MessageBox.Show("Cannot read file from disk: " +
ex.Message.ToString());
}
}
}
private void ShowFileInfo(string sFilePath)
{
// Part 1: File Version Information
System.Diagnostics.FileVersionInfo fileVersInfo =
System.Diagnostics.FileVersionInfo.GetVersionInfo(sFilePath);
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: " +
fi.CreationTime.ToLongDateString();
lblCreationTime.Text = "Creation Time: " +
fi.CreationTime.ToLongTimeString();
lblLastAccessDate.Text = "Last Access Date: " +
fi.LastAccessTime.ToLongDateString();
lblLastAccessTime.Text = "Last Access Time: " +
fi.LastAccessTime.ToLongTimeString();
lblLastWriteDate.Text = "Last Write Date:" +
fi.LastWriteTime.ToLongDateString();
lblLastWriteTime.Text = "Last Write Time:" +
fi.LastWriteTime.ToLongTimeString();
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;
}