Working with Files

There's a parallel set of classes to Directory and DirectoryInfo that enables you to work with files File and FileInfo . The File class works with files in general, and the FileInfo class works with a specific file. Like Directory , File is a static class, and you can see the significant public methods of this class in Table 5.4, which let you create files, delete them, move them, and more.

Table 5.4. Significant Static Public File Methods

METHOD

PURPOSE

AppendText

Appends Unicode text to a file.

Copy

Copies a file to a new file.

Create

Creates a file using the given path .

CreateText

Creates or opens a file for writing.

Delete

Deletes the given file. (No exception is thrown if the given file does not exist.)

Exists

Determines whether the given file exists.

GetCreationTime

Returns the creation date and time of the given file or 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 written to last.

Move

Moves a given file to a new location.

Open

Opens a file stream for a file.

OpenRead

Opens an existing file for reading.

OpenText

Opens an existing text file for reading.

OpenWrite

Opens an existing file for writing.

SetCreationTime

Sets the date and time the file was created.

SetLastAccessTime

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

SetLastWriteTime

Sets the date and time that the given file was last written to.

You can see the significant public properties of the FileInfo class in Table 5.5 and its significant public methods in Table 5.6.

Table 5.5. Significant Public FileInfo Properties

PROPERTY

PURPOSE

CreationTime

Returns or sets the creation time of the current FileSystemInfo object.

Exists

Returns true if a file exists, false otherwise .

Extension

Returns the string representing the extension part of the file.

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 when the current file or directory was written to last.

Length

Returns the size of a file.

Name

Returns the name of the file.

Table 5.6. Significant Public FileInfo Methods

METHOD

PURPOSE

AppendText

Appends text to the file.

CopyTo

Copies an existing file to a new one.

Create

Creates a file.

CreateText

Creates a StreamWriter object that writes a new text file.

Delete

Deletes a file.

MoveTo

Moves a given file to a new location.

Open

Opens a file.

OpenRead

Creates a read-only FileStream object.

OpenText

Creates a StreamReader that reads from an existing text file.

OpenWrite

Creates a write-only FileStream object.

Refresh

Refreshes the state of the object.

Here's an example using the FileInfo class to determine the name, length, and last modified date of all the files in the current directory. You can use the GetFiles method of a DirectoryInfo object to get an array of FileInfo objects. We loop over that array in ch05_02.cs, Listing 5.2, displaying the filenames and last-modified times.

Listing 5.2 Getting File Information (ch05_02.cs)
 using System.IO; class ch05_02 {   public static void Main()   {     string currentDirectory =       System.Environment.CurrentDirectory;     DirectoryInfo dir = new DirectoryInfo(currentDirectory);     FileInfo[] files = dir.GetFiles();  foreach (FileInfo file in files)   {   System.Console.WriteLine(   "{0} is {1} bytes long, last modified {2}.",   file.Name, file.Length, file.LastWriteTime);   }  } } 

Here's the kind of display you see when you run ch05_02.cs:

 
 C:\>ch05_02 ch05_02.cs is 518 bytes long, last modified 2/17 2:00:04 PM. ch05_02.exe is 3584 bytes long, last modified 2/17 2:00:06 PM. 

Here's another example, using the DirectoryInfo method CreateSubdirectory to create a new subdirectory named backup , and the FileInfo CopyTo method to copy all files in the current directory to the backup directory. You can see how this code works in ch05_03.cs, Listing 5.3.

Listing 5.3 Copying Files (ch05_03.cs)
 using System.IO; class ch05_03 {   public static void Main()   {     string currentDirectory =       System.Environment.CurrentDirectory;     DirectoryInfo dir = new DirectoryInfo(currentDirectory);  DirectoryInfo backupDirectory =   dir.CreateSubdirectory("backup");   FileInfo[] files = dir.GetFiles();   foreach (FileInfo file in files)   {   string newName = backupDirectory.FullName +   @"\" + file.Name;   file.CopyTo(newName);   }  } } 

As you can see, the Directory , DirectoryInfo , File , and FileInfo classes enable you to work with directories and files without actually handing the data inside a file. As such, they provide only external services when handling your data; to open files and access their contents directly, you use streams.



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

Similar book on Amazon

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