Chapter 13 - File IO and Isolated Storage pps

69 413 0
Chapter 13 - File IO and Isolated Storage pps

Đ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

Chapter 13 File I/O and Isolated Storage Hoang Anh Viet VietHA@it-hut.edu.vn Hanoi University of Technology Objectives “The System.IO namespace allows you to interact with a machine’s file and directory structure Over the course of this chapter, you will learn how to programmatically create (and destroy) a directory system as well as move data into and out of various streams (file based, string based, memory based, etc.) The latter part of this chapter examines the role of isolated storage, which allows you to persist per-user data into a safe sandbox, regardless of the security settings of a target machine To understand certain aspects of the System.IO.IsolatedStorage API, you will also receive an overview of Code Access Security (CAS).…” Microsoft Roadmap Exploring the System.IO namespace Working with File System The Abstract System Class Working with StreamWriters and StreamReaders Working with StringWriters and StringReaders Working with BinaryWriters and BinaryReaders Programmatically “Watching” Files Performing Asynchronous File I/O An Overview of Isolated Storage 10 Introducing Object Serialization Microsoft 13.1 Exploring the System.IO namespace   System.IO namespace is the region of the base class libraries devoted to file-based (and memory-based) input and output (I/O) services System.IO defines a set of classes, interfaces, enumerations, structures, and delegates, most of which are contained in mscorlib.dll Microsoft Figure 13.1 Key Members of the System.IO Namespace Nonabstract I/O Class Type Description BinaryReader, BinaryWriter These types allow you to store and retrieve primitive data types (integers, Booleans, strings, and whatnot) as a binary value BufferedStream This type provides temporary storage for a stream of bytes that may be committed to storage at a later time Directory, DirectoryInfo These types are used to manipulate a machine’s directory structure The Directory type exposes functionality using static members The DirectoryInfo type exposes similar functionality from a valid object reference DriveInfo This type provides detailed information regarding the drives used by a given machine File, FileInfo These types are used to manipulate a machine’s set of files The File type exposes functionality using static members The FileInfo type exposes similar functionality from a valid object reference FileStream This type allows for random file access (e.g., seeking capabilities)with data represented as a stream of bytes Microsoft Figure 13.1 Key Members of the System.IO Namespace (Cont.) Nonabstract I/O Class Type Description FileSystemWatcher This type allows you to monitor the modification of external files in a specified directory MemoryStream This type provides random access to streamed data stored in memory rather than a physical file Path This type performs operations on System.String types that contain file or directory path information in a platform-neutral manner StreamWriter, StreamReader These types are used to store (and retrieve) textual information to (or from) a file These types not support random file access StringWriter, StringReader Like the StreamReader/StreamWriter types, these classes also work with textual information However, the underlying storage is a string buffer rather than a physical file Microsoft Roadmap Exploring the System.IO namespace Working with File System The Abstract System Class Working with StreamWriters and StreamReaders Working with StringWriters and StringReaders Working with BinaryWriters and BinaryReaders Programmatically “Watching” Files Performing Asynchronous File I/O An Overview of Isolated Storage 10 Introducing Object Serialization Microsoft 13.2 Working with File System  System.IO namespace include a few basic classes for retrieving file system information     Directory and File: These classes provide static methods that allow you to retrieve information about any files and directories that are visible from your server DriveInfo, DirectoryInfo, FileInfo: These classes use similar instance methods and properties to retrieve the same information The simplest level of file access:   Microsoft Involves retrieving information about existing files and directories Performing typical file system operations such as copying files and creating directories Classes File and Directory Figure 13.2 The File- and Directory-centric types Microsoft Figure 13.3 Directory Methods Method Description CreateDirectory() Creates a new directory If you specify a directory inside another nonexistent directory, ASP.NET will thoughtfully create all the required directories Delete() Deletes the corresponding empty directory To delete a directory along with its contents (subdirectories and files), add the optional second parameter of true Exists() Returns true or false to indicate whether the specified directory exists GetCreationTime(), GetLastAccessTime(), and GetLastWriteTime() Returns a DateTime object that represents the time the directory was created, accessed, or written to Each “Get” method has a corresponding “Set” method, which isn’t shown in this table GetDirectories() and GetFiles() Returns an array of strings, one for each subdirectory or file in the specified directory These methods can accept a second parameter that specifies a search expression (such as ASP*.*) GetLogicalDrives() Returns an array of strings, one for each drive that’s defined on the current computer Drive letters are in this format: c:\ Microsoft 10 Example 13.8 Asynchronously interact with a FileStream type 55 Roadmap Exploring the System.IO namespace Working with File System The Abstract System Class Working with StreamWriters and StreamReaders Working with StringWriters and StringReaders Working with BinaryWriters and BinaryReaders Programmatically “Watching” Files Performing Asynchronous File I/O An Overview of Isolated Storage 10 Introducing Object Serialization Microsoft 56 13.9 An Overview of Isolated Storage  One major reason for the isolated storage API is to provide a safe sandbox where applications can read/write data, regardless of which code group they are placed into and without the need to alter the default security policies     Microsoft Save preferences for your application for each user or forms of per-user data (DataSets, XML files, etc.) Deploy a ClickOnce application that functions in a sandbox and has no access to the (unrestrained) local file system Download Windows Forms controls from a URL that integrate into a web application, and they must store settings on the client machine Deploy a XAML Browser Application, or XBAP that needs to persist data on the user’s machine 57 The Scope of Isolated Storage   By its very nature, isolated storage will also persist data using (at very least) the current user as a level of isolation In addition to user-level partitioning, isolated storage can also be set up to further isolate data based on the assembly and/or application domain identity Figure 13.15 User and assembly-level isolation Microsoft 58 The Scope of Isolated Storage  On the other hand, if configure a user + assembly + application domain isolation level, the application domain is also taken into consideration  In addition to the most specific form of assembly evidence, the most specific form of AppDomain evidence (typically Site) will be used to build the store Figure 13.16 User, assembly, and application domain-level isolation Microsoft 59 The Type of System.IO.IsolatedStorage System.IO.IsolatedStorage Type Meaning in Life IsolatedStorage This type represents the abstract base class from which all isolated storage implementations must derive IsolatedStorageScope This enum controls the level of isolation to make use of (assembly, application domain, roaming) IsolatedStorageException This type specifies the exception that is thrown when an operation in isolated storage fails IsolatedStorageFile This type represents an isolated storage area containing files and Directories IsolatedStorageFileStream This type exposes a file within isolated storage Figure 13.17 The Types of System.IO.IsolatedStorage Microsoft 60 Figure 13.18 Members of IsolatedStorageFile Member Meaning in Life CurrentSize, MaximumSize These read-only properties allow you to view size characteristics of isolated storage Scope This property shows the scope of isolation (user, assembly, AppDomain) CreateDirectory() This method creates a new directory in the store DeleteDirectory() This method deletes a directory from the store DeleteFile() This method deletes a file within a given directory GetDirectoryNames() This method allows you to iterate over named directories GetEnumerator() This method gets a scope-specific IEnumerator GetFiles() This method gets files within a specific store GetStore() This overloaded method obtains isolated storage corresponding to the given application domain and assembly evidence objects and isolated storage scope GetUserStoreForAssembly() This method obtains isolated storage corresponding to the calling code’s assembly identity GetUserStoreForDomain() This method obtains isolated storage corresponding to the application domain identity and assembly identity Remove() Microsoft This method removes stores 61 Roadmap Exploring the System.IO namespace Working with File System The Abstract System Class Working with StreamWriters and StreamReaders Working with StringWriters and StringReaders Working with BinaryWriters and BinaryReaders Programmatically “Watching” Files Performing Asynchronous File I/O An Overview of Isolated Storage 10 Introducing Object Serialization Microsoft 62 13.10 Introducing Object Serialization   The term serialization describes the process of persisting (and possibly transferring) the state of an object into a stream (file stream, memory stream, etc.) When an object is serialized, the CLR will account for all related objects to ensure the data is persisted correctly This set of related objects is referred to as an object graph Microsoft 63 Defining Serializable Types  Class Radio Microsoft 64 JamesBondCar and Car classes Microsoft 65 Serializing Objects Using the BinaryFormatter  The two key methods of the BinaryFormatter type to be aware of are Serialize() and Deserialize():   Serialize(): Persists an object graph to a specified stream as a sequence of bytes Deserialize(): Converts a persisted sequence of bytes to an object graph Using System.Runtime.Serialization.Formatters.Binary namespace  Microsoft 66 Serializing Objects Using the SoapFormatter   The SoapFormatter will persist an object graph into a SOAP message Using System.Runtime.Serialization.Formatters.Soap namespace Microsoft 67 Summary  Directory(Info) and File(Info) allow you to manipulate a physical file or directory on your hard drive    Examined a number of types derived from the abstract Stream class, specifically FileStream Checked out the functionality provided by DriveType, how to monitor files using the FileSystemWatcher type and how to interact with streams in an asynchronous manner Introduced you to the topic of isolated storage  Microsoft This API allows a program to read and write data in a safe sandbox 68 References  Books:  Microsoft Andrew Troelsen, “Pro C# 2008 and the NET 3.5 Platform” 69 ... operations such as copying files and creating directories Classes File and Directory Figure 13. 2 The File- and Directory-centric types Microsoft Figure 13. 3 Directory Methods Method Description... Files Performing Asynchronous File I/O An Overview of Isolated Storage 10 Introducing Object Serialization Microsoft 13. 1 Exploring the System .IO namespace   System .IO namespace is the region... class libraries devoted to file- based (and memory-based) input and output (I/O) services System .IO defines a set of classes, interfaces, enumerations, structures, and delegates, most of which

Ngày đăng: 02/08/2014, 09:20

Từ khóa liên quan

Mục lục

  • Chapter 13. File I/O and Isolated Storage

  • Objectives

  • Roadmap

  • 13.1 Exploring the System.IO namespace

  • Slide 5

  • Slide 6

  • Slide 7

  • 13.2 Working with File System

  • Classes File and Directory

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Example 13.1 Displaying the name of each file in the current directory

  • Slide 15

  • Example

  • The DirectoryInfo and FileInfo Classes

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan