Exploring the System.IO namespace - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- Exploring the System.IO namespace (http://www.webasp.net/article/4/3968.htm) |
| -- 作者:未知 -- 发布日期: 2003-07-12 |
| 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.
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.
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:
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() + "<br>"); Response.Write(reader.ReadLine()+ "<br>"); Response.Write(reader.ReadLine() + "<br>"); Response.Write(reader.ReadLine()+ "<br>"); Response.Write(reader.ReadLine() + "<br>"); Response.Write(reader.ReadLine()+ "<br>"); //Close the reader reader.Close(); } |
| webasp.net |