Getting Information About A File

This example presents the FileInfo class, which allows you to get information about a file. Listing 1 presents the FileInfo class in use. This program takes a single filename and displays the size and key dates regarding it. For the output, the FileSize.cs file was used.

Listing 1. FileSize.cs — Using the FileInfo Class

 1: //  FileSize.cs -  
 2: //-----------------------------------------------
 3: using System;
 4: using System.IO;
 5: 
 6: class FileSize
 7: {
 8:   public static void Main()
 9:   {
10:      string[] CLA = Environment.GetCommandLineArgs();
11:       
12:      FileInfo fiExe = new FileInfo(CLA[0]);
13: 
14:      if ( CLA.Length < 2 )
15:      {
16:          Console.WriteLine("Format: {0} filename", fiExe.Name);
17:      }
18:      else
19:      {
20:        try
21:        {
22:          FileInfo fiFile = new FileInfo(CLA[1]);
23: 
24:          if(fiFile.Exists)
25:          {
26:            Console.WriteLine("===================================");
27:            Console.WriteLine("{0} - {1}", 
                                      fiFile.Name, fiFile.Length );
28:            Console.WriteLine("===================================");
29:            Console.WriteLine("Last Access: {0}",
                                      fiFile.LastAccessTime);
30:            Console.WriteLine("Last Write:  {0}",
                                      fiFile.LastWriteTime);
31:            Console.WriteLine("Creation:    {0}", 
                                      fiFile.CreationTime);
32:            Console.WriteLine("===================================");
33:          }
34:          else
35:          {
36:            Console.WriteLine("{0} doesn't exist!", fiFile.Name);
37:          }
38:       }
39:  
40:       catch (System.IO.FileNotFoundException)
41:       {
42:         Console.WriteLine("\n{0} does not exist!", CLA[1]);
43:         return;
44:       }
45:       catch (Exception e)
46:       {
47:         Console.WriteLine("\nException thrown trying to copy file.");
48:         Console.WriteLine(e);
49:         return;
50:       }
51:     }
52:   }
53: }

The following is the output from this listing. Your output will vary depending on when you create the FileSize.cs program.


===================================
FileSize.cs - 1551
===================================
Last Access: 2/22/2003 11:40:30 PM
Last Write:  2/22/2003 11:40:19 PM
Creation:    2/22/2003 11:39:45 PM

The FileInfo class creates an object that is associated to a specific file. In line 12, a FileInfo object was created called fiExe that is associated to the program being executed (fileinfo.exe). If the user doesn't enter an argument on the command line, the value of fiExe is printed with program usage information (line 14).

In line 22, a second FileInfo object is created using the argument passed to the program. In lines 26 to 32, information is displayed about this file. As you can see, the information displayed includes the file size using the Length member, the creation date and time using the CreationTime member, the last access date and time using the LastAccessTime member, and finally the last date and time the file was written to using the LastWriteTime member.

For more information

For more information check out my C# book, Sams Teach Yourself C# in 21 Days.



About the Author

Bradley Jones

Bradley Jones, in addition to managing CodeGuru, Brad! oversees the Developer.com Newtwork of sites including Codeguru, Developer.com, DevX, VBForums, and over a dozen more with a focus on software development and database technologies. His experience includes development in C, C++, VB, some Java, C#, ASP, COBOL, and more as well as having been a developer, consultant, analyst, lead, and much more. His recent books include Teach Yourself the C# Language in 21 Days, Web 2.0 Heroes, and Windows Live Essentials and Services.
Google+ Profile | Linked-In Profile | Facebook Page

Comments

  • CreationTime better for SET than GET

    Posted by rox.scott on 03/25/2005 12:07pm

    I've found the CreationTime property useless for finding the actual creation time of files, unless I set it myself. This is because of Windows 'File Tunneling' behaviour. http://www.devcity.net/Articles/133/1/article.aspx

    Reply
  • File Version, Description, etc.?

    Posted by Legacy on 03/18/2003 12:00am

    Originally posted by: Marty

    Using .NET, how would you go about enumerating the version information fields for the same file? I looked through the .NET Class Library a little bit but didn't find anything.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds