I/O ClassesTo understand how to carry out the reading and writing of data using the I/O classes, we will create a new ASP.NET web application in Visual C# using the
File -> New Project dialog box. We will name the project
IOExamples as shown in the following diagram:
We will kick off our demonstration of the I/O classes by considering the classes that are related to performing file system operations.
File System OperationsAs discussed before, the classes that are used to browse around the file system and perform operations like moving, copying, and deleting files, can be broadly classified into two categories.
- Classes that expose static or shared methods, e.g. File and Directory. If you want to only do operations on a folder or a file then using the File or Directory class is more efficient as it saves us the overhead of instantiating a .NET class.
- Classes that expose instance methods. e.g. FileInfo and DirectoryInfo. These classes can be used to perform the same set of functions as the File and Directory classes except that we need to create an instance of them to be able to do so. These classes will be very efficient if you are performing multiple operations using the same object, because they will read in the authentication and other information for the appropriate file system object at the time of the construction of the object. Once this information is read and stored as part of the object, it need not be read every time a method call is made.
To demonstrate the implementation of the file system related classes, let us consider the following example:
When the user selects the file using the
Browse dialog box, and clicks
Get Attributes, we execute the code shown below.
We start by getting the name of the selected file from the form element named fileOpener, and putting it into a local variable named fileName. Then we create an instance of the FileInfo object, passing to it the full path of the selected file. We then invoke its properties and methods to display the attributes of the selected file.
private void bntGetAttributes_Click(object sender, System.EventArgs e)
{
//Clear the listbox
lstAttributes.Items.Clear();
//Get the name of the selected file
string fileName = fileOpener.Value;
//Set the label to display the selected file
lblMessage.Text = "Selected file is :" + fileName;
FileInfo fileInfo = new FileInfo(fileName);
lstAttributes.Items.Add("The file attributes are : " );
lstAttributes.Items.Add(" Name = " + fileInfo.Name);
lstAttributes.Items.Add(" Creation Time = " + fileInfo.CreationTime);
lstAttributes.Items.Add(" Last Access Time = " +
fileInfo.LastAccessTime.ToString());
lstAttributes.Items.Add(" Directory Name = " + fileInfo.DirectoryName);
lstAttributes.Items.Add(" Length = " + fileInfo.Length.ToString());
lstAttributes.Items.Add("...........");
We then get the name of the parent directory of the selected file, and create an instance of the DirectoryInfo object using the directory name as the argument. Once we create an instance of the DirectoryInfo object, we can then display the attributes of the directory.
string dirName = fileInfo.DirectoryName;
DirectoryInfo dirInfo = new DirectoryInfo(dirName);
lstAttributes.Items.Add("The Directory attributes are : " );
lstAttributes.Items.Ad