14
Files and Streams 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

Chapter 17

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Chapter 17

Files and Streams

• 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

Page 2: Chapter 17

File Information System

The simplest level of file access involves retrieving information about existing files and directories and performing typical file system operations such as copying files and creating directories.

.NET provides five basic classes for retrieving this sort of information. They are all located in the System.IO namespace

• The Directory and File classes, which provide static methods that allow you to retrieve information about any files and directories visible from your server

• The DirectoryInfo and FileInfo classes, which use similar instance methods and properties to retrieve the same sort of information

• The DriveInfo class, which provides static methods that allow you to retrieve information about a drive and the amount of free space it provides

Page 3: Chapter 17

The Path Class

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 absolutePath = @"c:\Users\MyDocuments";string subPath = @"Sarah\worksheet.xls";string combined = Path.Combine(absolutePath, subPath);

string file = Path.GetFileName( @"c:\Documents\Upload\Users\JamesX\resume.doc");

Page 4: Chapter 17

Methods in Path Class

Page 5: Chapter 17

The Directory and File Classes

The Directory and File classes provide a number of useful static methods.

Most of these methods take the same parameter: afully qualified path name identifying the directory or file you want the operation to act on.

A few methods, such as Delete() and Move(), take additional parameters.

Page 6: Chapter 17

The DirectoryInfo and FileInfo Classes

The DirectoryInfo and FileInfo classes mirror the functionality in the Directory and File classes. In addition, they make it easy to walk through directory and file relationships. For example, you can easily retrieve the FileInfo objects for the files in a directory represented by a DirectoryInfo object.

Note that while the Directory and File classes expose only methods, DirectoryInfo and FileInfo provide a combination of properties and methods. For example, while the File class had separate GetAttributes() and SetAttributes() methods, the FileInfo class includes an Attributes property.

Another nice thing about the DirectoryInfo and FileInfo classes is that they share a common set of properties and methods because they derive from the common FileSystemInfo base class.

Page 7: Chapter 17

DriveInfo Class

The DriveInfo class allows you to retrieve information about a drive on your computer.

Typically, the DriveInfo class is merely used to retrieve the total amount of used and free space.

Unlike the FileInfo and DriveInfo classes, there’s noDrive class with instance versions of these methods.

Page 8: Chapter 17

DriveInfo Members

Page 9: Chapter 17

Reading and Writing with Streams

You can write to a file and read from a file using a StreamWriter and a StreamReader—dedicated classes that abstract away the process of file interaction.

You can use one of the helpful static methods included in the 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();

Page 10: Chapter 17

Reading and Writing with StreamsTo read the information, you use the corresponding StreamReader class. It provides a ReadLine() method that gets the next available value and returns it as a string. ReadLine() starts at the first line and advances 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();

Page 11: Chapter 17

For multiple user working on same file

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 same time, an exception will occur. You can reduce this problem when opening files using the more generic four-parameter version 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 a FileStream object, and you must manually create a StreamReader that wraps it.

Here’s the code you need to create a multiuser-friendly StreamReader:FileStream fs = File.Open(@"c:\myfile.txt", FileMode.Open, FileAccess.Read,FileShare.Read);StreamReader r = new StreamReader(fs);

Page 12: Chapter 17

Shortcuts for Reading and Writing Files

.NET includes functionality for turbo-charging your file writing and 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");

Page 13: Chapter 17

File Methods for quick input and output

Page 14: Chapter 17

ASP.NET includes FileUpload Control that allows website users to upload files to the web server.

To get information about the posted file content, you can access the FileUpload.PostedFile object. You can save the content by calling the PostedFile.SaveAs() method:

Uploader.PostedFile.SaveAs(@"c:\Uploads\newfile");

Allowing File Uploads