Subscribe

RSS Feed (xml)

Retrieving Information About File or Directory

To retrieve information such as attributes or creation date.

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. Below Table lists the members that are declared in FileInfo or DirectoryInfo classes.
Table: 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



Technorati :

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail