1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning Microsoft Visual C# 2008 PHẦN 7 pptx

135 267 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

c23.indd 774c23.indd 774 3/24/08 5:19:44 PM3/24/08 5:19:44 PM Part IV Data Access Chapter 24: File System Data Chapter 25: XML Chapter 26: Introduction to LINQ Chapter 27: LINQ to SQL Chapter 28: ADO.NET and LINQ over DataSet Chapter 29: LINQ to XML c24.indd 775c24.indd 775 3/25/08 12:20:10 PM3/25/08 12:20:10 PM c24.indd 776c24.indd 776 3/25/08 12:20:10 PM3/25/08 12:20:10 PM 24 File System Data Reading and writing files are essential aspects of many .NET applications. This chapter shows you how, touching on the major classes used to create, read from, and write to files, and the supporting classes used to manipulate the file system from C# code. Although you won ’ t examine all of the classes in detail, this chapter goes into enough depth to give you a good idea of the concepts and fundamentals. Files can be a great way to store data between instances of your application, or they can be used to transfer data between applications. User and application configuration settings can be stored to be retrieved the next time your application is run. Delimited text files, such as comma - separated files, are used by many legacy systems, and to interoperate with such systems you need to know how to work with delimited data. As you will see, the .NET Framework provides you with the necessary tools to use files effectively in your applications. By the end of this chapter, you will have learned the following: What a stream is and how .NET uses stream classes to access files . How to use the File object to manipulate the file structure . How to write to, and read from, a file . How to read and write formatted data from and to files . How to read and write compressed files . How to serialize and deserialize objects . How to monitor files and directories for changes . ❑ ❑ ❑ ❑ ❑ ❑ ❑ c24.indd 777c24.indd 777 3/25/08 12:20:10 PM3/25/08 12:20:10 PM Part IV: Data Access 778 Streams All input and output in the .NET Framework involves the use of streams . A stream is an abstract representation of a serial device . A serial device is something that stores data in a linear manner and is accessed the same way: one byte at a time. This device can be a disk file, a network channel, a memory location, or any other object that supports reading and writing to it in a linear manner. Keeping the device abstract means that the underlying destination/source of the stream can be hidden. This level of abstraction enables code reuse, and enables you to write more generic routines because you don ’ t have to worry about the specifics of how data transfer actually occurs. Therefore, similar code can be transferred and reused when the application is reading from a file input stream, a network input stream, or any other kind of stream. Because you can ignore the physical mechanics of each device, you don ’ t need to worry about, for example, hard disk heads or memory allocation when dealing with a file stream. There are two types of streams: Output: Output streams are used when data is written to some external destination, which can be a physical disk file, a network location, a printer, or another program. Understanding stream programming opens many advanced possibilities. This chapter focuses on file system data, so you ’ ll only be looking at writing to disk files. Input: Input streams are used to read data into memory or variables that your program can access. The most common form of input stream you have worked with so far is the keyboard. An input stream can come from almost any source, but this chapter focuses on reading disk files. The concepts applied to reading/writing disk files apply to most devices, so you ’ ll gain a basic understanding of streams and see a proven approach that can be applied to many situations. The Classes for Input and Output The System.IO namespace contains almost all of the classes that you will be covering in this chapter. System.IO contains the classes for reading and writing data to and from files, and you can reference this namespace in your C# application to gain access to these classes without fully qualifying type names. Quite a few classes are contained in System.IO , as shown in Figure 24 - 1 , but you will only be working with the primary classes needed for file input and output. ❑ ❑ c24.indd 778c24.indd 778 3/25/08 12:20:11 PM3/25/08 12:20:11 PM Chapter 24: File System Data 779 The classes covered in this chapter are described in the following table: Class Description File A static utility class that exposes many static methods for moving, copying, and deleting files . Directory A static utility class that exposes many static methods for moving, copying, and deleting directories . Path A utility class used to manipulate path names . FileInfo Represents a physical file on disk, and has methods to manipulate this file. For any reading and writing to the file, a Stream object must be created. DirectoryInfo Represents a physical directory on disk and has methods to manipulate this directory . Object (System) MarshalByRefObject (System) System (System.IO) FileStream (System.IO) Directory (System.IO) File (System.IO) Component (System) FileSystemWatcher (System.IO) FileSystemInfo (System.IO) FileInfo (System.IO) DirectoryInfo (System.IO) TextReader (System.IO) TextWriter (System.IO) StreamReader (System.IO) StreamWriter (System.IO) Path (System.IO) Figure 24-1 (continued) c24.indd 779c24.indd 779 3/25/08 12:20:11 PM3/25/08 12:20:11 PM Part IV: Data Access 780 Class Description FileSystemInfo Serves as the base class for both FileInfo and DirectoryInfo , making it possible to deal with files and directories at the same time using polymorphism . FileStream Represents a file that can be written to or read from, or both. This file can be written to and read from asynchronously or synchronously. StreamReader Reads character data from a stream and can be created by using a FileStream as a base . StreamWriter Writes character data to a stream and can be created by using a FileStream as a base . FileSystemWatcher The most advanced class you will examine in this chapter. It is used to monitor files and directories, and exposes events that your application can catch when changes occur in these locations. This functionality has always been missing from Windows programming, but now the .NET Framework makes it much easier to respond to file system events. You ’ ll also look at the System.IO.Compression namespace, which enables you to read and write compressed files, by using either GZIP compression or the Deflate compression scheme: DeflateStream — Represents a stream in which data is compressed automatically when writing, or uncompressed automatically when reading. Compression is achieved using the Deflate algorithm. GZipStream — Represents a stream in which data is compressed automatically when writing, or uncompressed automatically when reading. Compression is achieved using the GZIP algorithm. Finally, you ’ ll explore object serialization using the System.Runtime.Serialization namespace and its child namespaces. You ’ ll primarily be looking at the BinaryFormatter class in the System.Runtime.Serialization.Formatters.Binary namespace, which enables you to serialize objects to a stream as binary data, and deserialize them again. The File and Directory Classes The File and Directory utility classes expose many static methods for manipulating, surprisingly enough, files and directories. These methods make it possible to move files, query and update attributes, and create FileStream objects. As you learned in Chapter 8, static methods can be called on classes without having to create instances of them. ❑ ❑ c24.indd 780c24.indd 780 3/25/08 12:20:11 PM3/25/08 12:20:11 PM Chapter 24: File System Data 781 Some of the most useful static methods of the File class are shown in the following table: Method Description Copy() Copies a file from a source location to a target location . Create() Creates a file in the specified path . Delete() Deletes a file . Open() Returns a FileStream object at the specified path . Move() Moves a specified file to a new location. You can specify a different name for the file in the new location. Some useful static methods of the Directory class are shown in the next table: Method Description CreateDirectory() Creates a directory with the specified path . Delete() Deletes the specified directory and all the files within it . GetDirectories() Returns an array of string objects that represent the names of the directories below the specified directory . GetFiles() Returns an array of string objects that represent the names of the files in the specified directory . GetFileSystemEntries() Returns an array of string objects that represent the names of the files and directories in the specified directory . Move() Moves the specified directory to a new location. You can specify a new name for the folder in the new location. The FileInfo Class Unlike the File class, the FileInfo class is not static and does not have static methods. This class is only useful when instantiated. A FileInfo object represents a file on a disk or a network location, and you can create one by supplying a path to a file: FileInfo aFile = new FileInfo(@”C:\Log.txt”); Because you will be working with strings representing the path of a file throughout this chapter, which means a lot of \ characters in your strings, remember that the @ that prefixes the preceding string means that this string will be interpreted literally. Thus, \ will be interpreted as \ , and not as an escape character. Without the @ prefix, you would need to use \\ instead of \ to avoid having this character be interpreted as an escape character. In this chapter you ’ ll stick to the @ prefix for your strings. c24.indd 781c24.indd 781 3/25/08 12:20:12 PM3/25/08 12:20:12 PM Part IV: Data Access 782 You can also pass the name of a directory to the FileInfo constructor, although in practical terms that isn ’ t particularly useful, as it causes the base class of FileInfo , which is FileSystemInfo , to be initialized with all the directory information, but none of the FileInfo methods or properties relating specifically to files will work. Many of the methods exposed by the FileInfo class are similar to those of the File class, but because File is a static class, it requires a string parameter specifying the file location for every method call. Therefore, the following calls do the same thing: FileInfo aFile = new FileInfo(“Data.txt”); if (aFile.Exists) Console.WriteLine(“File Exists”); if (File.Exists(“Data.txt”)) Console.WriteLine(“File Exists”); In this code a check is made to see whether the file Data.txt exists. Note that no directory information is specified here, meaning that the current working directory is the only location examined. This directory is the one containing the application that calls this code. You ’ ll look at this in more detail a little later, in the section “ Path Names and Relative Paths. ” Most of the FileInfo methods mirror the File methods in this manner. In most cases it doesn ’ t matter which technique you use, although the following criteria may help you to decide which is more appropriate: It makes sense to use methods on the static File class if you are only making a single method call — the single call will be faster because the .NET Framework will not have to go through the process of instantiating a new object and then calling the method. If your application is performing several operations on a file, it makes more sense to instantiate a FileInfo object and use its methods — this saves time because the object will already be referencing the correct file on the file system, whereas the static class will have to find it every time. The FileInfo class also exposes properties relating to the underlying file, some of which can be manipulated to update the file. Many of these properties are inherited from FileSystemInfo , and thus apply to both the File and Directory classes. The properties of FileSystemInfo are shown in the following table: Property Description Attributes Gets or sets the attributes of the current file or directory, using the FileAttributes enumeration . CreationTime Gets or sets the creation date and time of the current file . Extension Retrieves the extension of the file. This property is read - only. ❑ ❑ c24.indd 782c24.indd 782 3/25/08 12:20:12 PM3/25/08 12:20:12 PM Chapter 24: File System Data 783 Property Description Exists Determines whether a file exists. This is a read - only abstract property, and is overridden in FileInfo and DirectoryInfo . FullName Retrieves the full path of the file. This property is read - only. LastAccessTime Gets or sets the date and time that the current file was last accessed . LastWriteTime Gets or sets the date and time that the current file was last written to . Name Retrieves the full path of the file. This is a read - only abstract property, and is overridden in FileInfo and DirectoryInfo . The properties specific to FileInfo are shown in the next table: Property Description Directory Retrieves a DirectoryInfo object representing the directory containing the current file. This property is read - only. DirectoryName Returns the path to the file ’ s directory. This property is read - only. IsReadOnly Shortcut to the read - only attribute of the file. This property is also accessible via Attributes . Length Gets the size of the file in bytes, returned as a long value. This property is read - only. A FileInfo object doesn ’ t in itself represent a stream. To read or write to a file, a Stream object has to be created. The FileInfo object aids you in doing this by exposing several methods that return instantiated Stream objects. The DirectoryInfo Class The DirectoryInfo class works exactly like the FileInfo class. It is an instantiated object that represents a single directory on a machine. Like the FileInfo class, many of the method calls are duplicated across Directory and DirectoryInfo . The guidelines for choosing whether to use the methods of File or FileInfo also apply to DirectoryInfo methods: If you are making a single call, use the static Directory class. If you are making a series of calls, use an instantiated DirectoryInfo object. ❑ ❑ c24.indd 783c24.indd 783 3/25/08 12:20:13 PM3/25/08 12:20:13 PM [...]... opened, with the stream positioned at the beginning of the file An exception is thrown OpenOrCreate The file is opened, with the stream positioned at the beginning of the file A new file is created Truncate The file is opened and erased The stream is positioned at the beginning of the file The original file creation date is retained An exception is thrown 78 5 c24.indd 78 5 3/25/08 12:20:14 PM Part IV: Data... string strLine; string[] strArray; char[] charArray = new char[] {‘,’}; List data = new List(); columns = new List(); try 79 7 c24.indd 79 7 3/25/08 12:20: 17 PM Part IV: Data Access { FileStream aFile = new FileStream(@” \ \SomeData.txt”, FileMode.Open); StreamReader sr = new StreamReader(aFile); // Obtain the columns from the first line //... /Program.cs”, FileMode.Open); aFile.Seek(113, SeekOrigin.Begin); aFile.Read(byData, 0, 200); } catch(IOException e) { Console.WriteLine(“An IO exception has been thrown!”); Console.WriteLine(e.ToString()); 78 7 c24.indd 78 7 3/25/08 12:20:14 PM Part IV: Data Access Console.ReadKey(); return; } Decoder d = Encoding.UTF8.GetDecoder(); d.GetChars(byData, 0, byData.Length, charData, 0); Console.WriteLine(charData); Console.ReadKey();... StreamWriter sw = new StreamWriter(aFile); bool truth = true; // Write data to file 79 2 c24.indd 79 2 3/25/08 12:20:16 PM Chapter 24: File System Data sw.WriteLine(“Hello to you.”); sw.WriteLine(“It is now {0} and things are looking good.”, DateTime.Now.ToLongDateString()); sw.Write(“More than that,”); sw.Write(“ it’s {0} that C# is fun.”, truth); sw.Close(); } catch(IOException e) { Console.WriteLine(“An... convert this date into an easy-to-read form 79 3 c24.indd 79 3 3/25/08 12:20:16 PM Part IV: Data Access The Write()method simply writes the string passed to it to the file, without a newline character appended, enabling you to write a complete sentence or paragraph using more than one Write()statement: sw.Write(“More than that,”); sw.Write(“ it’s {0} that C# is fun.”, truth); Here, again, you use format... FileStream constructor will throw an exception when it doesn’t find it 5 Run the application You should see the text of the file written to the console, as shown in Figure 24-5 Figure 24-5 79 5 c24.indd 79 5 3/25/08 12:20: 17 PM Part IV: Data Access How It Works This application is very similar to the previous one, with the obvious difference being that it is reading a file, rather than writing one As before,... the size of the data file, this can be prohibitive If the data file is extremely large, then it is better to leave the data in the file and access it with the methods of the StreamReader 79 6 c24.indd 79 6 3/25/08 12:20: 17 PM Chapter 24: File System Data Delimited Files Delimited files are a common form of data storage and are used by many legacy systems If your application must interoperate with such a... Read(), and Encoder instead of Decoder The following line creates a character array by using the ToCharArray()static method of the String class Because everything in C# is an object, the text “My pink half of the drainpipe.” is 79 0 c24.indd 79 0 3/25/08 12:20:15 PM Chapter 24: File System Data actually a string object (albeit a slightly odd one), so these static methods can be called even on a string of... following code to the Main()method: static void Main(string[] args) { List columns; List myData = GetData(out columns); foreach (string column in columns) { 79 8 c24.indd 79 8 3/25/08 12:20: 17 PM Chapter 24: File System Data Console.Write(“{0,-20}”, column); } Console.WriteLine(); foreach (Dictionary row in myData) { foreach (string column in columns) { Console.Write(“{0,-20}”,... Note that these two lines were enclosed in try catch blocks to handle any exceptions that may be thrown: try { aFile.Seek(135, SeekOrigin.Begin); aFile.Read(byData,0,100); } catch(IOException e) 78 8 c24.indd 78 8 3/25/08 12:20:15 PM Chapter 24: File System Data { Console.WriteLine(“An IO exception has been thrown!”); Console.WriteLine(e.ToString()); Console.ReadKey(); return; } Almost all operations . ADO.NET and LINQ over DataSet Chapter 29: LINQ to XML c24.indd 77 5c24.indd 77 5 3/25/08 12:20:10 PM3/25/08 12:20:10 PM c24.indd 77 6c24.indd 77 6 3/25/08 12:20:10 PM3/25/08 12:20:10 PM 24 File System. files and directories for changes . ❑ ❑ ❑ ❑ ❑ ❑ ❑ c24.indd 77 7c24.indd 77 7 3/25/08 12:20:10 PM3/25/08 12:20:10 PM Part IV: Data Access 77 8 Streams All input and output in the .NET Framework. classes needed for file input and output. ❑ ❑ c24.indd 77 8c24.indd 77 8 3/25/08 12:20:11 PM3/25/08 12:20:11 PM Chapter 24: File System Data 77 9 The classes covered in this chapter are described

Ngày đăng: 09/08/2014, 14:21