Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Visual C# » Capturing File Information

Capturing File Information

This article describes a simple approach to capturing and displaying file and file version information.

Author Rank :
Page Views : 25250
Downloads : 722
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
GetFileInformation.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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());

        }

    }

}

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 =

    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;

}

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.

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Ecelent by chandrasekhar On August 17, 2007
nice
Reply | Email | Modify 
files and arrays by Kay On May 14, 2010
Pure genius!
I need to know how to add files to an array and then print all the documents in that array as one print job, or atleast so it appears as one job on the print spooler. If its at all possible, any help would be appreciated. Thanks, Kay
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.