Exploring the System.IO namespace

80酷酷网    80kuku.com

  I/O Classes
To 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 Operations
As 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.Add(" Name = " + dirInfo.Name);
   lstAttributes.Items.Add(" Root Name = " + dirInfo.Root.Name);
   lstAttributes.Items.Add("...........");
   lstAttributes.Items.Add("All the files contained
                         in the directory are : " );
We also display all of the child files contained in the directory by looping through the array of FileInfo objects returned by the GetFiles method.
   foreach(FileInfo fileInfoChild in dirInfo.GetFiles())
   {
      lstAttributes.Items.Add(fileInfoChild.Name);
   }

   lstAttributes.Items.Add("...........");
   lstAttributes.Items.Add("All the subdirectories
              contained in the directory are : " );
Finally we display all the directories contained in the directory by enumerating through the DirectoryInfo 's array returned by the GetDirectories method.
   foreach(DirectoryInfo dirInfoChild in dirInfo.GetDirectories())
   {
      lstAttributes.Items.Add(dirInfoChild.Name);
   }
}
This method is used to copy over the selected file from its current directory to the application path.
private void bntCopyFile_Click(object sender, System.EventArgs e)
{
   //Clear the listbox
   lstAttributes.Items.Clear();
   //Get the full path of the selected file
   string sourcePath = fileOpener.Value;
   //Set the label to display the selected file
   lblMessage.Text = "Selected file is :"  + sourcePath;
   //Get the file name alone from the string
   string fileName = Path.GetFileName(sourcePath);
   string destPath =
               Server.MapPath(Request.ApplicationPath) + "\\" + fileName ;
   //Copy the file to the current application path
   File.Copy(sourcePath,destPath,true);
   lstAttributes.Items.Add(" The file " + fileName +
                         " has been copied from " );
   lstAttributes.Items.Add(sourcePath);
   lstAttributes.Items.Add("   to   " + destPath);
}
We use this method to move the selected file from its current location to the application path.
private void btnMoveFile_Click(object sender, System.EventArgs e)
{
   //Clear the listbox
   lstAttributes.Items.Clear();
   //Get the full path of the selected file
   string sourcePath = fileOpener.Value;
   //Set the label to display the selected file
   lblMessage.Text = "Selected file is :"  + sourcePath;
   string fileName = Path.GetFileName(sourcePath);
   string destPath = Server.MapPath(Request.ApplicationPath) +
                                             "\\" + fileName ;
   //Move the file to the current application path
   File.Move(sourcePath,destPath);
   lstAttributes.Items.Add(" The file " + fileName +
                          " has been moved from " );
   lstAttributes.Items.Add(sourcePath);
   lstAttributes.Items.Add("   to   " + destPath);
}
In this method, we get the selected file and delete it from its current location.
private void btnDeleteFile_Click(object sender, System.EventArgs e)
{
   //Clear the listbox
   lstAttributes.Items.Clear();
   //Get the full path of the selected file
   string sourcePath = fileOpener.Value;
   //Set the label to display the selected file
   lblMessage.Text = "Selected file is :"  +  sourcePath;
   string fileName = Path.GetFileName(sourcePath);
   //Delete the file
   File.Delete(sourcePath);
   lstAttributes.Items.Add(" The file " + fileName +
                        " has been deleted from " );
   lstAttributes.Items.Add(sourcePath);
}
Path
Since the Path class contains only static methods, its methods can be accessed without having to create an instance of it. The methods exposed by the Path class are mainly used to manipulate path names in a platform-independent manner. Using the Path class is a whole lot easier than trying to fiddle about with path separation characters manually, especially because the Path class encapsulates the different formats for path names on different operating systems.
The following example illustrates the different important properties and methods of the Path class. The web form we are going to use for the demonstration looks like this:

Once you select the file name using the Browse file open dialog box and click Get Path attributes, we execute the following lines of code:
private void bntGetAttributes_Click(object sender, System.EventArgs e)
{
   //Clear the listbox
   lstAttributes.Items.Clear();
   string fileName = fileOpener.Value;
   //Set the label to display the file name
   lblMessage.Text = "The file you have selected is " + fileName;
   //Display the properties in the listbox
   lstAttributes.Items.Add(" File name = : " + Path.GetFileName(fileName));
   lstAttributes.Items.Add(" Directory Name = " +
                  Path.GetDirectoryName(fileName));
   lstAttributes.Items.Add(" Extension = " + Path.GetExtension(fileName));
   lstAttributes.Items.Add(" File name without extension = " +
                     Path.GetFileNameWithoutExtension(fileName));
   lstAttributes.Items.Add(" Root Path = " +
   Path.GetPathRoot(fileName));lstAttributes.Items.Add(" Has Extension = " +
                                    Path.HasExtension(fileName).ToString());
   lstAttributes.Items.Add(" Path Separator = " + Path.PathSeparator);
   lstAttributes.Items.Add("Directory Separator = " +
                        Path.DirectorySeparatorChar);
   lstAttributes.Items.Add("Alt Directory Separator = " +
                           Path.AltDirectorySeparatorChar);
   lstAttributes.Items.Add("Temporary folder name = " + Path.GetTempPath());
}
In the code presented above, we get the name of the file that the user has selected by using the following line:
string fileName = fileOpener.Value;
Once we get the file name, we pass this as an argument to the static members of the Path class and show the results in the list box.
BinaryReader and BinaryWriter
When we work with streams of binary data, we often need to read and write primitive types. The System.IO namespace provides BinaryReader and BinaryWriter classes to accomplish this. Using the BinaryWriter class, we can not only write data that belongs to any of the primitive types, but also specify the type of encoding to be used for writing.
Similar to the BinaryWriter class, the BinaryReader exposes properties and methods to read primitive data types as binary values. Some of the important methods that provide this functionality include: ReadBoolean, ReadByte, ReadChar, ReadDecimal, ReadDouble, ReadInt16, ReadInt32, ReadInt64, ReadString, ReadSingle, and so on. From the names of the methods, we can easily figure out the kind of data each method is designed to handle.
Let us consider the following example for the illustration of writing of data using the BinaryWriter class. In this example, we write primitive types such as a string, an int, a single, and a decimal to the file.
private void Page_Load(object sender, System.EventArgs e)
{
   // Put user code to initialize the page here
   string fileName = Server.MapPath("binaryfile.txt");
   //Create the FileStream object with the proper settings
   FileStream stream = new
         FileStream(fileName,FileMode.Append,FileAccess.Write);
   //Associate the filestream with the BinaryReader
   BinaryWriter writer = new BinaryWriter(stream);
   writer.Write("This is a string  value");
   writer.Write(1233);
   writer.Write(1.8f);
   writer.Write(90.1m);
   stream.Flush();
   stream.Close();
}
To read the file created by the above example, we will use the BinaryReader class. Here we invoke the different forms of Read method such as ReadString, ReadInt16, ReadSingle, and ReadDecimal to read primitive types from the file. We then display the read contents in the list box named lstContents:
private void bntReadData_Click(object sender, System.EventArgs e)
{
   string fileName = Server.MapPath("binaryfile.txt");
   //Create the FileStream object with the proper settings
   FileStream stream = new
               FileStream(fileName,FileMode.Open,FileAccess.Read);
   //Associate the filestream with the BinaryReader
   BinaryReader reader = new BinaryReader(stream);
   //Read the primitive types from the file
   lstContents.Items.Add(reader.ReadString());
   lstContents.Items.Add(reader.ReadInt16().ToString());
   lstContents.Items.Add(reader.ReadSingle().ToString());
   lstContents.Items.Add(reader.ReadDecimal().ToString());
   stream.Flush();
   stream.Close();
}
StreamReader and StreamWriter
As mentioned before, these classes are used to read and write data that is in character format. They are mainly useful in cases when we are trying to read from or write data to a text file. When constructing a StreamReader, we need to provide values for the following options.
  • Encoding method: in this parameter, we need to specify what to do about the different encoding methods. There are two ways by which we can do this. We can either let the StreamReader examine the byte order marks in the file to determine the encoding method, or we can simply tell the StreamReader to assume that the file uses a specified encoding method.
  • Reference to another stream: instead of supplying a file name to be read from, we can supply a reference to another stream. This option is very important as it demonstrates the advantage of basing our model for reading and writing data around the concept of streams. This is useful in situations where you have another stream that contains the actual data, and you want to process the data contained in that stream in your application. We can easily carry this out by passing the output of that stream to a StreamReader constructor and start using it.

For example, the following line of code shows how to construct a simple StreamReader object. We pass in the file name to be read and the encoding to be assumed as arguments, to the constructor of the StreamReader object.
StreamReader reader = new StreamReader("c:\test.txt",Encoding.UTF8);
In the later stages of this article, when we look at the FileStream class, we will see how we can hook a StreamReader object up to a FileStream object to read data from a text file.
The following list shows some of the important methods of the StreamReader class:
  • Read - Allows us to read the next character or a set of next characters from the input stream.
  • ReadBlock - Allows us to read a block of characters into the buffer.
  • ReadToEnd - As the name suggests, this provides the ability to read character data from the current stream until the end.
  • ReadLine - Permits us to read a single line of characters from the current stream.
  • Peek - It is similar to the Read method except for the difference that it does not advance the current character position.


The StreamWriter class works basically the same as the StreamReader class, except that it is used to write to a text file. The StreamWriter class exposes the following methods to accomplish this: Write and WriteLine.
The following simple example shows how to read and write to a text file using the StreamReader and StreamWriter classes.
First off, we create a StreamWriter object and associate it with a file named StreamWriterFile.txt. We then write few lines of text using the WriteLine method. Finally we flush the contents and close the StreamWriter object.
private void Page_Load(object sender, System.EventArgs e)
{
   //Create the StreamWriter object with the proper settings
   StreamWriter writer = new
      StreamWriter(Server.MapPath("StreamWriterFile.txt"),true);
   writer.WriteLine("Programming Languages :");
   writer.WriteLine("ASP.NET");
   writer.WriteLine("Visual C#");
   writer.WriteLine("Visual Basic.NET");
   writer.WriteLine("JScript.NET");
   writer.WriteLine("ADO.NET");
   writer.Flush();
   //Close the StreamWriter
   writer.Close();
In the following block of code, we read the lines of text written by the above code and write them to the client browser:
   //Create the StreamReader object
   StreamReader reader = new
               StreamReader(Server.MapPath("StreamWriterFile.txt"));
   Response.Write(reader.ReadLine() + "
");
   Response.Write(reader.ReadLine()+ "
");
   Response.Write(reader.ReadLine() + "
");
   Response.Write(reader.ReadLine()+ "
");
   Response.Write(reader.ReadLine() + "
");
   Response.Write(reader.ReadLine()+ "
");
   //Close the reader
   reader.Close();
}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: