The FileInfo Class


The FileInfo Class

When you create a new FileInfo object, you must associate this object with a file by passing a string representation of a filename to the FileInfo constructor. This filename can either represent an already existing file, or a non-existing file that you want to create. We will look at how you can create a new file with FileInfo later.

Suppose that C:\MyTestFiles\MyFairytale.txt is an already existing file. The following line (taken from line 10 in Listing 22.1)

 FileInfo existingFile = new FileInfo(@"C:\MyTestFiles\MyFairytale.txt"); 

creates a FileInfo object called existingFile that represents the file MyFairytale.txt located in the C:\MyTestFiles directory.

Why Use @ in Front of Filenames?


Why does the previous line of source code include the symbol @ in front of the string containing the filename? You might recall from Chapter 7, "Types Part II: Operators, Enumerators, and Strings," that the @ symbol turns a string into a verbatim string, which lets us write \ instead of the cumbersome \\ when we specify pathnames.

We can now use the various FileInfo class members, a selected few of which are displayed in Table 22.2, to manipulate and ask questions about the underlying file (MyFairytale.txt, in this case).

Table 22.2. A Few FileInfo Class Members
FileInfo class member Description
CopyTo Copies the file represented by the FileInfo object to a new file
Delete Deletes the file represented by the FileInfo object
FullName Returns the full name (including the full path) of the file represented by the FileInfo object
Length Returns the size (in bytes) of the file represented by the FileInfo object
Name Returns the short name (excluding the path) of the file represented by the FileInfo object



To see the entire list of FileInfo class members, please consult the .NET Framework documentation.



Before you start using FileInfo and the rest of the classes in System.IO, be aware that many of these classes have the potential to wipe out or damage important files on your computer in case, for example, you provide the wrong filename or the wrong command. To protect your important files, you should keep any test files separate from other files by creating a test directory called, say, MyTestFiles. All the files used in coming examples are supposed to exist in a C:\MyTestFiles directory (where C is an arbitrary letter), but you can use any directory you prefer, as long as you remember to adjust the directory letters and the rest of the file path in your source code accordingly.

Whether you are experimenting with files or not, you should always keep backup files of the important files on your computer.

Listing 22.1 demonstrates how to use the FileInfo class members shown in Table 22.2 on an existing file, which in this case is called MyFairytale.txt. To run the program successfully, you must first create and save a file called MyFairytale.txt in the MyTestFiles directory by using the Notepad editor. The contents of this file, which you can simply write in the Notepad window, should be

 Once upon a time there was a beautiful princess 

You are now ready to compile and run the source code of Listing 22.1.

Listing 22.1 SimpleFileInfo.cs
01: using System; 02: using System.IO; 03: 04: class FileInspector 05: { 06:     public static void Main() 07:     { 08:         try 09:         { 10:             FileInfo existingFile = new FileInfo (@"C:\MyTestFiles\MyFairytale.txt"); 11: 12:             Console.WriteLine("File name: " + existingFile.Name); 13:             Console.WriteLine("Full file name: " + existingFile.FullName); 14:             Console.WriteLine("File length: { 0}  bytes", existingFile.Length); 15:             Console.WriteLine("Date creation time: " + existingFile.CreationTime); 16: 17:             Console.WriteLine("Making a copy of MyFairyTale.txt called  graphics/ccc.gifYourFairytale.txt"); 18:             existingFile.CopyTo(@"C:\MyTestFiles\YourFairytale.txt", true); 19:             Console.WriteLine("Deleting MyFairytale.txt"); 20:             existingFile.Delete(); 21:         } 22:         catch (IOException exObj) 23:         { 24:             Console.WriteLine(exObj); 25:         } 26:     } 27: } File name: MyFairytale.txt Full file name: C:\MyTestFiles\MyFairytale.txt File length: 48 bytes Date creation time: 24/08/2001 11:08:54 PM Making a copy of MyFairyTale.txt called YourFairytale.txt Deleting MyFairytale.txt 

Line 2 has been included to save us from prefixing FileInfo with its namespace name System.IO.

A file is often kept over a long period of time, during which several programs and programmers might have access to this same file. As a result, the file is prone to be mistakenly deleted, renamed, or reformatted. For example, if you try to access a non-existent file, the runtime will throw an exception. As a result, exceptions can be thrown relatively easily when you work with files, so it is important to use C#'s exception handling mechanisms for dealing with these exceptions appropriately, as discussed in Chapter 19, "Exception Handling." Accordingly, Listing 22.1 contains the familiar try-catch construct.

Line 10, discussed earlier, creates the FileInfo object existingFile that represents the C:\MyTestFiles\MyFairyTale.txt file. Lines 12 15 simply access and print out information about the represented file by using some of the FileInfo members listed in Table 22.2.

Line 18 makes a copy of the file called C:\MyTestnkFiles\YourFairytale.txt. The second argument, true, provided to CopyTo, allows the copy to overwrite any existing file with the same name.

Finally, line 20 deletes the file C:\MyTestFiles\MyFairytale.txt represented by existingFile.

After running this program, your MyTestFiles directory should contain only one file called YourFairytale.txt with the same two lines of text we originally included in MyFairytale.

FileInfo Versus File

 File.Copy(@"C:\MyTestFiles\MyFairytale.txt", @"C:\MyTestFiles\YourFairytale.txt"); 

without any prior object creation, as would have been necessary with FileInfo. Why then bother using FileInfo? Every time you call a method with the File class, a processor-intensive security check must be performed, even if you access the same file repeatedly with different File methods. In contrast, the FileInfo class only performs one security check when the FileInfo object is created. After that, all processing related to the underlying file can be performed without security checks.

Full and Relative Filenames


A filename that includes the root directory and the full path to the file is called a full (or fully qualified) filename. For example, C:\MyTestFiles\MyFairytale.txt is a full filename. If the path is omitted, as in MyFairytale.txt, the filename is interpreted relative to the current directory. For example, if the program in Listing 22.1 was running from the C:\MyC#Programs\Ch22 directory, and line 18 only specified the relative filename YourFairytale.txt, the program would save YourFairytale.txt in the C:\MyC#programs\Ch22 directory.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: