Tải bản đầy đủ - 0 (trang)
5-1. Retrieve Information About a File, Directory, or Drive

5-1. Retrieve Information About a File, Directory, or Drive

Tải bản đầy đủ - 0trang

CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



Member



Applies To



Description



DirectoryName and

Directory



FileInfo



DirectoryName returns the name of the parent directory as

a string. Directory returns a full DirectoryInfo object that

represents the parent directory and allows you to retrieve

more information about it.



Parent and Root



DirectoryInfo



Return 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.



EnumerateFiles



DirectoryInfo



Returns an IEnumerable of FileInfo objects, with one

element for each file contained in this directory



EnumerateDirectories



DirectoryInfo



Returns an IEnumerable of DirectoryInfo objects, with one

element for each subdirectory.



DriveType



DriveInfo



Returns a DriveType enumeration value that represents

the type of the specified drive; for example, Fixed or CD

Rom.



AvailableFreeSpace



DriveInfo



Returns a long that represents the free space available in

the drive.



GetDrives



DriveInfo



Returns an array of DriveInfo objects that represents the

logical drives in the computer.



The following are a few points to note while working with these objects:





FileInfo and DirectoryInfo classes derive from the abstract FileSystemInfo class,

which defines common methods like CreationTime, Exists, and so on. The

DriveInfo class does not inherit from this base class, so it does not provide some

of the common members available in the other two classes.



207



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O







The full set of properties that FileInfo and DirectoryInfo objects expose is read

the first time you interrogate any property. If the file or directory changes after this

point, you must call the Refresh method to update the properties. However, this is

not the case for DriveInfo; each property access asks the file system for an up-todate value.







You will not encounter an error if you specify a path that does not correspond to

an existing file, directory, or drive. Instead, you will receive an object that

represents an entity that does not exist—its Exists (or IsReady property for

DriveInfo) property will be false. You can use this object to manipulate the entity.

However, if you attempt to read most other properties, exceptions like

FileNotFoundException, DirectoryNotFoundException, and so on will be thrown.



The Code

The following console application takes a file path from a command-line argument, and then displays

information about the file, the containing directory, and the drive.

using System;

using System.IO;

namespace Apress.VisualCSharpRecipes.Chapter05

{

static class Recipe05_01

{

static void Main(string[] args)

{

if (args.Length == 0)

{

Console.WriteLine("Please supply a filename.");

return;

}

// Display file information.

FileInfo file = new FileInfo(args[0]);

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



208



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



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

// Display drive information.

DriveInfo drv = new DriveInfo(file.FullName);

Console.Write("Drive: ");

Console.WriteLine(drv.Name);

if (drv.IsReady)

{

Console.Write("Drive type: ");

Console.WriteLine(drv.DriveType.ToString());

Console.Write("Drive format: ");

Console.WriteLine(drv.DriveFormat.ToString());

Console.Write("Drive free space: ");

Console.WriteLine(drv.AvailableFreeSpace.ToString());

}

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}



209



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



Usage

If you execute the command Recipe05-01.exe c:\windows\win.ini, you might expect the following

output:

Checking file: win.ini

File exists: True

File created: 31.Mar.2003 5:30:00 PM

File last updated: 24.Sep.2005 11:11:13 PM

File last accessed: 10.Nov.2005 9:41:05 PM

File size (bytes): 658

File attribute list: Archive



Checking directory: windows

In directory: c:\

Directory exists: True

Directory created: 04.Jun.2005 4:47:56 PM

Directory last updated: 01.Nov.2005 10:09:45 AM

Directory last accessed: 11.Nov.2005 6:24:59 AM

Directory attribute list: Directory

Directory contains: 134 files



210



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



Drive: c:\

Drive type: Fixed

Drive format: NTFS

Drive free space: 14045097984



5-2. Set File and Directory Attributes

Problem

You need to test or modify file or directory attributes.



Solution

Create a System.IO.FileInfo object for a file or a System.IO.DirectoryInfo object for a directory and use

the bitwise AND (&) and OR (|) arithmetic operators to modify the value of the Attributes property.



How It Works

The FileInfo.Attributes and DirectoryInfo.Attributes properties represent file attributes such as

archive, system, hidden, read-only, compressed, and encrypted. Because a file can possess any

combination of attributes, the Attributes property accepts a combination of enumerated values. To

individually test for a single attribute or change a single attribute, you need to use bitwise arithmetic.

FileInfo.Attributes and DirectoryInfo.Attributes both return values from the FileAttributes

enumeration, whose most commonly used values are





ReadOnly (the file is read-only)







Hidden (the file is hidden from ordinary directory listings)







System (the file part of the operating system)







Directory (the file is a directory)







Archive (used by backup applications)







Temporary (this is a temporary file and will be deleted when no longer required)







Compressed (the contents of the file are compressed)







Encrypted (the contents of the file are encrypted)



211



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



The Code

The following example takes a read-only test file and checks for the read-only attribute:

using System;

using System.IO;

namespace Apress.VisualCSharpRecipes.Chapter05

{

static class Recipe05_02

{

static void Main()

{

// This file has the archive and read-only attributes.

FileInfo file = new FileInfo(@"C:\Windows\win.ini");

// This displays the attributes.

Console.WriteLine(file.Attributes.ToString());

// This test fails because other attributes are set.

if (file.Attributes == FileAttributes.ReadOnly)

{

Console.WriteLine("File is read-only (faulty test).");

}

// This test succeeds because it filters out just the

// read-only attribute.

if ((file.Attributes & FileAttributes.ReadOnly) ==

FileAttributes.ReadOnly)

{

Console.WriteLine("File is read-only (correct test).");

}

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}

When setting an attribute, you must also use bitwise arithmetic, as demonstrated in the following

example. In this case, it’s needed to ensure that you don’t inadvertently clear the other attributes.

// This adds just the read-only attribute.

file.Attributes = file.Attributes | FileAttributes.ReadOnly;

// This removes just the read-only attribute.

file.Attributes = file.Attributes & ~FileAttributes.ReadOnly;



212



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5-1. Retrieve Information About a File, Directory, or Drive

Tải bản đầy đủ ngay(0 tr)

×