Working with Directories and Files in .NET
page 3 of 6
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 28011/ 92

Working with Files

Similar to the Directory and the DirectoryInfo class that we use when working with Directories, you have the File and the FileInfo classes for working with files in C#. The File class contains a number of static methods that can be used for creating, opening, copying, deleting, and moving of files in your file system. The members of the File class are as follows.

·         Copy

·         Exists

·         Open

·         Delete

·         Move

·         Create

·         CreateText

·         AppendText

·         GetCreationTime

·         GetLastAccessTime

·         GetLastWriteTime

The following code snippet illustrates how you can use the File class to check whether a file exists and if so, it sets the creation time, last access time and the last write time of the file. It then deletes the file using the Delete() method of the File class.

Listing 2

if (File.Exists(@"C:\Test.txt"))
  {
   File.SetCreationTime(@"C:\Test.txt", Date.Now);
   File.SetLastAccessTime(@"C:\Test.txt", Date.Now);
   File.SetLastWriteTime(@"C:\Test.txt", Date.Now);
  }
 
  if (File.Exists(@"C:\Test.txt"))
   File.Delete(@"C:\Test.txt");

Please note that I have provided you some of the most important members of the classes Directory, DirectoryInfo, File and FileInfo. You can refer to MSDN for a complete list of the members of each of these classes.

Unlike the File class, the FileInfo class contains not only static methods, but the FileInfo class also contains instance methods to perform various file operations in your file system. Note that the FileInfo class is abstract in nature. The following is the list of members of the FileInfo class.

·         Open

·         Create

·         Delete

·         CopyTo

·         MoveTo

·         CreateText

·         AppendText

Further to this, you have some enumerators that contain fields that correspond to the file modes, file share and the file access permissions, etc. The following is the complete list of the enumerators in the System.IO namespace.

·         DriveType

·         FileAccess

·         FileAttributes

·         FileMode

·         FileOption

·         FileShare

·         NotifyFilters

·         SearchOption

·         SeekOrigin

·         WatcherChangeTypes

I will discuss only the FileMode, FileShare, and FileAccess enumerators here. You can refer to the MSDN for information on the other enumerators.

The available file modes that are contained in the FileMode enumerator are the following.

·         Open

·         Truncate

·         Create

·         CreateNew

·         Append

The truncate file mode is used to indicate that an existing file should be opened and the contents of the file should be truncated so that its size becomes bytes. I will skip a discussion on the other file modes as their names are self explanatory and should be well understood by the reader. The FileShare enumerator is used to specify the level of access for a file in the file system that is currently in use. The FileShare enumerator, "Contains constants for controlling the kind of access other FileStream objects can have to the same file." The available fields in the FileShare enumerator are listed here.

·         None

·         Read

·         ReadWrite

·         Delete

·         Write

The FileAccess enumerator contains fields that correspond to read, write and read/write access to a file in the file system. The following are the fields in the FileAccess enumerator.

·         Read

·         Write

·         ReadWrite


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-02 8:25:17 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search