Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chapter 17


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Chapter 17

  1. 1. • File System Information• The Path Class• The Directory and File Classes• The DirectoryInfo and FileInfo Classes• The DriveInfo Class• Reading and Writing with Streams• The File Upload Control
  2. 2. The simplest level of file access involves retrieving informationabout existing files and directories and performing typical file systemoperations such as copying files and creating directories..NET provides five basic classes for retrieving this sort ofinformation. They are all located in the System.IO namespace• The Directory and File classes, which provide static methods thatallow you to retrieve information about any files and directoriesvisible from your server• The DirectoryInfo and FileInfo classes, which use similar instancemethods and properties to retrieve the same sort of information• The DriveInfo class, which provides static methods that allow youto retrieve information about a drive and the amount of free space itprovides
  3. 3. Along with the five classes outlined in the previous section, .NET also includes a helper class named Path in the same System.IO namespace. The Path class doesn’t include any real file management functionality. It simply provides a few static methods that are useful when manipulating strings that contain file and directory paths.string file = Path.GetFileName( @"c:DocumentsUploadUsersJamesXresume.doc"); string absolutePath = @"c:UsersMyDocuments"; string subPath = @"Sarahworksheet.xls"; string combined = Path.Combine(absolutePath, subPath);
  4. 4. The Directory and File classes provide a number of usefulstatic methods.Most of these methods take the same parameter: afully qualified path name identifying the directory or file youwant the operation to act on.A few methods, such as Delete() and Move(), takeadditional parameters.
  5. 5. The DirectoryInfo and FileInfo classes mirror the functionality inthe Directory and File classes.In addition, they make it easy to walk through directory and filerelationships. For example, you can easily retrieve the FileInfoobjects for the files in a directory represented by a DirectoryInfoobject.Note that while the Directory and File classes expose onlymethods, DirectoryInfo and FileInfo provide a combination ofproperties and methods. For example, while the File class hadseparate GetAttributes() and SetAttributes() methods, the FileInfoclass includes an Attributes property.Another nice thing about the DirectoryInfo and FileInfo classes isthat they share a common set of properties and methods becausethey derive from the common FileSystemInfo base class.
  6. 6. The DriveInfo class allows you to retrieve informationabout a drive on your computer.Typically, the DriveInfo class is merely used to retrieve thetotal amount of used and free space.Unlike the FileInfo and DriveInfo classes, there’s noDrive class with instance versions of these methods.
  7. 7. You can write to a file and read from a file using aStreamWriter and a StreamReader—dedicated classesthat abstract away the process of file interaction.You can use one of the helpful static methods included inthe File class, such as CreateText() or OpenText().Code to write a line to a text file:StreamWriter w;w = File.CreateText(@"c:myfile.txt");w.WriteLine("This file generated by ASP.NET");w.WriteLine(42);w.Close();
  8. 8. To read the information, you use the corresponding StreamReaderclass. It provides a ReadLine() method that gets the next availablevalue and returns it as a string. ReadLine() starts at the first line andadvances the position to the end of the file, one line at a time.StreamReader r =File.OpenText(@"c:myfile.txt");string inputString;inputString = r.ReadLine();inputString = r.ReadLine();string line;do{line = r.ReadLine();if (line != null){}} while (line != null);r.Close();
  9. 9. The code you’ve seen so far opens a file in single-user mode.If a second user tries to access the same file at the sametime, an exception will occur. You can reduce this problemwhen opening files using the more generic four-parameterversion of the File.Open() method instead of File.OpenText().You must specify FileShare.Read for the final parameter.Unlike the OpenText() method, the Open() method returns aFileStream object, and you must manually create aStreamReader that wraps it.Here’s the code you need to create a multiuser-friendlyStreamReader:FileStream fs =File.Open(@"c:myfile.txt", FileMode.Open, FileAccess.Read,FileShare.Read);StreamReader r = new StreamReader(fs);
  10. 10. .NET includes functionality for turbo-charging your file writingand reading.string[] lines = new string[]{"This is the first line of the file.","This is the second line of the file.","This is the third line of the file."};// Write the file in one shot.File.WriteAllLines(@"c:testfile.txt", lines);// Read the file in one shot (into a variable named content).string content = File.ReadAllLines(@"c:testfile.txt");
  11. 11. ASP.NET includes FileUpload Control that allowswebsite users to upload files to the web server.To get information about the posted file content, you canaccess the FileUpload.PostedFile object. You can savethe content by calling the PostedFile.SaveAs() method:Uploader.PostedFile.SaveAs(@"c:Uploadsnewfile");