9.1 Retrieving Information About a File or Directory


Problem

You need to retrieve information about a file or directory, such as its attributes or creation date.

Solution

Create a System.IO.FileInfo object for a file or a System.IO.DirectoryInfo object for a directory, supplying the path in the constructor. You can then retrieve information through the properties of the object.

Discussion

To create a FileInfo or a DirectoryInfo object, you simply supply a relative or fully qualified path in the constructor. You can retrieve information about the file or directory through the corresponding object properties. Table 9.1 lists the members that are declared in FileInfo or DirectoryInfo classes.

Table 9.1: Members for Files and Directories

Member

Applies To

Description

Exists

FileInfo and DirectoryInfo

Returns true or false , depending on whether a file or a directory exists at the specified location.

Attributes

FileInfo and DirectoryInfo

Returns one or more values from the System.IO.FileAttributes enumeration, which represents the attributes of the file or the directory.

CreationTime , LastAccessTime , and LastWriteTime

FileInfo and DirectoryInfo

Return System.DateTime instances that describe when a file or a directory was created, last accessed, and last updated, respectively.

FullName , Name , and Extension

FileInfo and DirectoryInfo

Returns a string that represents the fully qualified name, the directory or the file name (with extension), and the extension on its own.

Length

FileInfo

Returns the file size as a number of bytes.

DirectoryName and Directory

FileInfo

DirectoryName returns the name of the parent directory as a string , whereas Directory returns a full DirectoryInfo object that represents the parent directory and allows you to retrieve more information about it.

Parent and Root

DirectoryInfo

Returns a DirectoryInfo object that represents the parent or root directory.

CreateSubdirectory

DirectoryInfo

Creates a directory with the specified name in the directory represented by the DirectoryInfo object. It also returns a new DirectoryInfo object that represents the subdirectory.

GetDirectories

DirectoryInfo

Returns an array of DirectoryInfo objects, with one element for each subdirectory contained in this directory.

GetFiles

DirectoryInfo

Returns an array of FileInfo objects, with one element for each file contained in this directory.

There are two important considerations for working with FileInfo and DirectoryInfo objects:

  • The full set of FileInfo or DirectoryInfo object properties is read the first time you interrogate any property. If the file changes after this point, you must call the Refresh method to update the properties.

  • You won't encounter an error if you specify a path that doesn't correspond to an actual, existing file or directory when creating a FileInfo or DirectoryInfo object. Instead, you'll receive an object that represents a file or directory that doesn't exist ”its Exists property will be false . You can use this object to create the file or directory using the Create method. If you attempt to read most other properties, however, a FileNotFoundException or DirectoryNotFoundException is thrown.

The following Console application takes a file path from a command- line argument and then displays information about the file and the containing directory.

 using System; using System.IO; public class FileInformation {     private static void Main(string[] args) {         if (args.Length == 0) {             Console.WriteLine("Please supply a file name.");             return;         }         FileInfo file = new FileInfo(args[0]);         // Display file information.         Console.WriteLine("Checking file: " + file.Name);         Console.WriteLine("File exists: " + file.Exists.ToString());         if (file.Exists) {             Console.Write("File created: ");             Console.WriteLine(file.CreationTime.ToString());             Console.Write("File last updated: ");             Console.WriteLine(file.LastWriteTime.ToString());             Console.Write("File last accessed: ");             Console.WriteLine(file.LastAccessTime.ToString());             Console.Write("File size (bytes): ");             Console.WriteLine(file.Length.ToString());             Console.Write("File attribute list: ");             Console.WriteLine(file.Attributes.ToString());         }         Console.WriteLine();         // Display directory information.         DirectoryInfo dir = file.Directory;                      Console.WriteLine("Checking directory: " + dir.Name);         Console.WriteLine("In directory: " + dir.Parent.Name);         Console.Write("Directory exists: ");         Console.WriteLine(dir.Exists.ToString());         if (dir.Exists) {             Console.Write("Directory created: ");             Console.WriteLine(dir.CreationTime.ToString());             Console.Write("Directory last updated: ");             Console.WriteLine(dir.LastWriteTime.ToString());             Console.Write("Directory last accessed: ");             Console.WriteLine(dir.LastAccessTime.ToString());             Console.Write("Directory attribute list: ");             Console.WriteLine(dir.Attributes.ToString());             Console.WriteLine("Directory contains: " +                dir.GetFiles().Length.ToString() + " files");         }         Console.ReadLine();     } } 

If you execute the command FileInformation c:\ windows \win.ini here is the output you might expect:

 Checking file: win.ini File exists: True File created: 2001-08-23 8:00:00 AM File last updated: 2003-03-22 9:55:16 AM File last accessed: 2003-05-26 2:21:53 PM File size (bytes): 2128 File attribute list: Archive Checking directory: windows In directory: c:\ Directory exists: True Directory created: 2000-01-01 8:03:33 AM Directory last updated: 2003-05-26 2:25:25 PM Directory last accessed: 2003-05-26 2:25:25 PM Directory attribute list: Directory Directory contains: 147 files 
Note  

Instead of using the instance methods of the FileInfo and DirectoryInfo classes, you can use the static File and Directory classes. The File and Directory methods expose most of the same functionality, but they require you to submit the file name or path with every method invocation. In cases where you have to perform multiple operations with the same file or directory, using the FileInfo and DirectoryInfo classes will be faster, because they will perform security checks only once.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net