Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
World Class ASP.NET Hosting - 3 Month Free Hosting, Click Here!
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Visual Studio 2010 » Capturing File Information

Capturing File Information

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

Author Rank:
Technologies: .NET Compact Framework, .NET 1.0/1.1, Windows Forms,Visual C# .NET
Total downloads : 518
Total page views :  18248
Rating :
 5/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
GetFileInformation.zip
 
Become a Sponsor



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.


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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
GetFileInformation.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Ecelent by chandrasekhar On August 17, 2007
nice
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved