Working with Directories

This chapter covers file handling, so we'll start with the File and Directory classes, which enable you to work on files without opening them (you can copy a file to another directory, for example). After using the File and Directory classes to manipulate files and directories, we'll turn to the use of streams to handle the contents of files and other data streams directly.

There's a lot of support for working directly with directories in C#. For example, you can use the static members of the Environment class to get the current directory, the system directory, and so on. Here are some of the more useful members of the Environment class:

  • System.Environment.CurrentDirectory Returns or sets the (fully qualified) path of the current directory.

  • System.Environment.SystemDirectory Returns the (fully qualified) path of the system directory.

  • System.Environment.CommandLine Returns the command line used to start this application.

  • System.Environment.GetEnvironmentVariable("SystemRoot") Returns the (fully qualified) path of the system root directory.

  • System.Environment.GetEnvironmentVariable("windir") Returns the (fully qualified) path of the Windows directory.

Working with directories in C# revolves around the Directory and DirectoryInfo classes. The Directory class is a static class, and you can find its significant methods in Table 5.1, allowing you to delete, move, navigate, and determine whether a directory exists.

FOR C++ PROGRAMMERS

Although stream handling in C# is similar to what you see in C++ in theory, in practice you use different classes and methods in C#. C# does not support the standard C# streams such as cin , cout , istream , ostream , and so on, so this chapter is new material for the C++ programmer.


QUOTING PATHNAMES

The directory separator character in Windows pathnames, \ , can cause a lot of trouble in quoted strings because it's also the character you use for character escapes. If you want to avoid accidental escapes , use an ampersand, @ , to indicate those strings are literals, as in @"c:\c#\example5.cs" .


Table 5.1. Significant Static Public Directory Methods

METHOD

PURPOSE

CreateDirectory

Creates directories.

Delete

Deletes a directory (and its contents).

Exists

Determines whether the given path refers to an existing directory.

GetCreationTime

Returns the creation date and time of a directory.

GetCurrentDirectory

Returns the current working directory of the application.

GetDirectories

Returns the names of subdirectories in the given directory.

GetDirectoryRoot

Returns the volume and root information for a given path.

GetFiles

Returns the names of files in the given directory.

GetLastAccessTime

Returns the date and time the given file or directory was last accessed.

GetLastWriteTime

Returns the date and time the given file or directory was last written to.

GetLogicalDrives

Returns the names of the logical drives on this machine in the form "< drive letter >:\" .

GetParent

Returns the parent directory of the given path, including absolute and relative paths.

Move

Moves a file or a directory and its contents to a new location.

SetCreationTime

Sets the creation date and time for the given file or directory.

SetCurrentDirectory

Sets the application's current working directory to the given directory.

SetLastAccessTime

Sets the date and time the given file or directory was last accessed.

SetLastWriteTime

Sets the date and time a directory was last written to.

The DirectoryInfo class gives you more information about a particular directory; you create objects of this class for a specific directory, and that object will hold information about that directory. You can see its significant public properties in Table 5.2 and its significant public methods in Table 5.3.

Table 5.2. Significant Public DirectoryInfo Properties

PROPERTY

PURPOSE

Exists

Returns a value indicating whether the directory exists.

FullName

Returns the full path of the directory or file.

LastAccessTime

Returns or sets the time the current file or directory was last accessed.

LastWriteTime

Returns or sets the time the current file or directory was last written to.

Name

Returns the name of this DirectoryInfo instance.

Parent

Returns the parent directory of a given subdirectory.

Root

Returns the root portion of a path.

Table 5.3. Significant Public DirectoryInfo Methods

METHOD

PURPOSE

Create

Creates a directory.

CreateSubdirectory

Creates a subdirectory or subdirectories using the given path.

Delete

Deletes a DirectoryInfo object and its contents from a path.

GetDirectories

Returns the subdirectories of the current directory.

GetFiles

Returns a list of the files in the current directory.

MoveTo

Moves a DirectoryInfo object and its contents to a new path.

Here's an example that will display the names of all the subdirectories in the current directory. To do that, we need a DirectoryInfo object for the current directory, and you can pass the current directory's path to the DirectoryInfo class's constructor to create that DirectoryInfo object. Then you can determine the subdirectories of the current directory with the GetDirectories method, which returns an array of DirectoryInfo objects:

 
 using System.IO; string currentDirectory = System.Environment.CurrentDirectory; DirectoryInfo dir = new DirectoryInfo(currentDirectory); DirectoryInfo[] directories = dir.GetDirectories(); 

Now you can loop over the DirectoryInfo objects we got as you see in ch05_01.cs, Listing 5.1, displaying the name of each subdirectory in the current directory and when it was last accessed.

Listing 5.1 Getting Subdirectory Information (ch05_01.cs)
 using System.IO; class ch05_01 {   public static void Main()   {     string currentDirectory = System.Environment.CurrentDirectory;     DirectoryInfo dir = new DirectoryInfo(currentDirectory);     DirectoryInfo[] directories = dir.GetDirectories();  foreach (DirectoryInfo directory in directories)   {   System.Console.WriteLine("{0} was last accessed on {1}",   directory.Name, directory.LastAccessTime);   }  } } 

When you run ch05_01.cs, you'll see something like this (the subdirectories in the current directory are named ch01ch05 here):

 
 C:\>ch05_01 ch01 was last accessed on 2/11 ch02 was last accessed on 2/4 ch03 was last accessed on 2/7 ch04 was last accessed on 2/13 ch05 was last accessed on 2/17 


Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

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