ch11 preview cs

Upload: vishal-suryawanshi

Post on 05-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Ch11 Preview Cs

    1/6

    Programming the .NET Compact Framework in C# By Paul Yao & David Durant

    Chapter 11 Storage Page 1Copyright 2003 Paul Yao & David Durant

    Chapter 11Storage

    In this chapter we examine .NET Compact Frameworks capabilities for accessing localstorage, concentrating on File I/O and Registry access.

    Storage.......................................................................................................................................................... 1The Object Store ..............................................................................................................................2

    System Power States.......................................................................................................... 3Installable File Systems ......................................................................................................3

    The Windows CE File System .........................................................................................................4ROM based files...............................................................................................................................4RAM based files ............................................................................................................................... 5

    File I/O........................................................................................................................................................... 6The File and Directory Classes........................................................................................................6Byte Level I/O...................................................................................................................................7Higher Level I/O ...............................................................................................................................8Encoding and Decoding Data ........................................................................................................10Reader/Writer Stream Relationship ............................................................................................ 11Using the I/O Classes ....................................................................................................................11Text File I/O....................................................................................................................................13Binary File I/O ................................................................................................................................15Writing Binary Data ........................................................................................................................ 15Reading Binary Data...................................................................................................................... 17Binary I/O and Structures............................................................................................................... 17File I/O Summary...........................................................................................................................20

    Registry Access ..........................................................................................................................................20Opening a Registry Key ................................................................................................................. 21The Need for P/Invoke Wrappers ..................................................................................................21Mapping Registry Types to Managed Types .................................................................................22Opening and Accessing Registry Keys..........................................................................................23Reading Registry Values................................................................................................................ 24Writing Registry Values.................................................................................................................. 25Using Function Overloading for Registry Access ..........................................................................26Registry Utility Class ......................................................................................................................27Updating the NOTEPADCE Sample .............................................................................................. 31

    Conclusion ..................................................................................................................................................34

    Storage

    Storage is an area where Windows CE-powered devices and desktop systems differsignificantly. On desktop machines the primary storage media is disk, while on Windows CE-powered systems the primary storage media is RAM. Windows CE can support rotating

  • 7/31/2019 Ch11 Preview Cs

    2/6

    Programming the .NET Compact Framework in C# By Paul Yao & David Durant

    Chapter 11 Storage Page 2Copyright 2003 Paul Yao & David Durant

    magnetic storage media, but this is the exception rather than the rule1. Battery-powered mobiledevices in particular, such as a Pocket PC, are not suited to support power-hungry rotatingmedia. Thus, Windows CE storage centers on using RAM instead of disk, and so mobile devicestorage tends to be fast but small. Windows CE-powered devices use a combination of ROMand RAM. Most PocketPCs support removable storage, primarily in the form of Compact Flash(CF) cards or Secure Digital (SD) media. [Comment 11.1]

    Another difference from desktop systems is that a Windows CE operating system imageresides in a non-volatile media. This might be ROM (Read-Only Memory), or it could be flashmemory2. The ROM contains operating system components, applications, device drivers, andother supporting files needed for device operation. Microsoft provides the Platform Builder toolthat third-parties use to create specific operating system images. While Microsoft providesguidelines for what a Pocket PC or SmartPhone device should include, the actual content isdetermined by the device manufacturer who creates the actual operating system image. This isthe reason that smart devices from different manufacturers sometimes have a slightly differentset of applications and device drivers. [Comment 11.2]

    In spite of these differences at the physical media, there are many common features at ahigher level of abstraction. Both device and device storage support a hierarchical file system.And both device and desktop storage support a hierarchical registry. [Comment 11.3]

    The Object Store

    On a Windows CE-powered device, RAM is divided into two portions: program memory andthe object store. Program memory serves the purpose that most desktop programmersassociate with memory: providing the space for code to load and run, and the space for data tobe allocated, used, and reclaimed. [Comment 11.4]

    The other portion of RAM is the object store. The object store serves as the primary storagearea for Windows CE. The object store contains three things: a file system, the system registry,and CE property databases. The Compact Framework has built-in support for file I/O, but not forthe registry or property databases. To use the registry and CE property databases, a programcalls native functions using P/Invoke. [Comment 11.5]

    The maximum allowable size for the object store is 256MB. This maximum size is exactlyone-half the total RAM that can be supported by Windows CE, which is 512MB3. The maximumsize for individual files in the object store is 32MB. RAM not used for the object store is availablefor use as program memory. [Comment 11.6]

    This chapter covers two of the three types of objects in the object store. We cover the use ofCompact Framework classes to access the file system directories and files. In addition, wecover using P/Invoke to access native functions to access the system registry. What we do notcover are Windows CE property databases, although they are described in the accompanyingboxed discussion. [Comment 11.7]

    Windows CE Property Databases

    Most Windows CE features are ported from desktop Windows. There are,however, certain features which were created to meet a need not available from

    existing desktop technologies. Windows CE property databases are one of thosetechnologies. [Comment 11.8]

    1One exception is the IBM Microdrive; it uses rotating media in a Compact Flash (CF) form factor.

    2The HP website uses the term "Flash ROM" for the flash memory used in their iPaq Pocket PCs. The

    benefit of flash memory is that it enables field upgrades.3

    That memory is factory-installed. Windows CE does not support adding additional RAM.

    http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.1http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.1http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.2http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.2http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.3http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.3http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.4http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.4http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.5http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.5http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.6http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.6http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.7http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.7http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.8http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.8http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.8http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.7http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.6http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.5http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.4http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.3http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.2http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.1
  • 7/31/2019 Ch11 Preview Cs

    3/6

  • 7/31/2019 Ch11 Preview Cs

    4/6

    Programming the .NET Compact Framework in C# By Paul Yao & David Durant

    Chapter 11 Storage Page 4Copyright 2003 Paul Yao & David Durant

    packaged in the form factor of a Type II Compact Flash card, available in 340MB and 1GBstorage capacities. [Comment 11.16]

    A maximum of 256 installable file systems can be made available on a single Pocket PC orWindows CE device (the limit is 10 on Windows CE 3.0 devices such as the Pocket PC andPocket PC 2002). Many installable file systems are FAT-based, including FAT12, FAT16, andFAT32; as the legacy of MS-DOS/Windows lives on. [Comment 11.17]

    Files in an installable file system have a maximum size of 4GB, considerably more than the32MB maximum for object-store files. On a Pocket PC, a storage card file system might appearin the \Storage Card directory. At present, however, there are not standard names. Becausethis is a localizable name, you should avoid hard-coding directory names. [Comment 11.18]

    Pocket PC programmers can address this issue with a pair of native Win32 API functions:FindFirstFlashCard /FindNextFlashCard enumerate installed file systems. (See the

    FindMemoryCard sample application in chapter 4, Platform Invoke, for an example of callingthese functions). [Comment 11.19]

    Thus, the files accessible to your application can be located in ROM, RAM, or the additionaldevices mentioned above. They can be part of the native Windows CE file system or in aninstallable file system. [Comment 11.20]

    The Windows CE File System

    The Windows CE file system provides a unified view of all available storage devices,including: [Comment 11.21]

    Files and directories from the operating system image (ROM-based) [Comment 11.22]

    Files and directories in the object store (RAM-based) [Comment 11.23]

    Files and directories on installable file systems [Comment 11.24]

    The term used in the Windows CE Platform Builder documentation is seamless integration ofeach of these different files systems. [Comment 11.25]

    The architects of Windows CE simplified the file system structure from the legacy MS-DOS/Windows disk storage architecture. They eliminated drive letters so that Windows CE usesa simpler single-tree file system hierarchy. Instead of 26 starting possible starting points, thereis only one: the root directory ('\'). [Comment 11.26]

    Because Windows CE does not support a current directory for a process, you must fullyqualify all path names in your application. Aside from these differences, the file systemdirectory structure has similarities to desktop filesystems: a hierarchical filesystem with supportfor long file names. The maximum file path on Windows CE, just like on desktop file systems, is260 characters. [Comment 11.27]

    The Windows CE file system does support memory mapped files, thus providing amechanism for inter-process memory sharing. Like other capabilities that we discuss in thischapter, the Compact Framework does not support memory mapped files. Chapter 4, (PlatformInvoke), provides details on accessing native Windows CE functionality. [Comment 11.28]

    ROM based files

    Files located in ROM reside in the \windows directory, and are read-only by definition. Manyhave the system or hidden attribute, or both. At system boot time, the object store is initializedwith whatever directories may be required. At that time, ROM-based files can be mapped toother directories besides the \windows directory. [Comment 11.29]

    The Platform Builder packages the Windows CE operating system as a ROM image,compressing some of these files and leaving others uncompressed, as determined by the

    http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.16http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.16http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.17http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.17http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.18http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.18http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.19http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.19http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.20http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.20http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.21http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.21http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.22http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.22http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.23http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.23http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.24http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.24http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.25http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.25http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.26http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.26http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.27http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.27http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.28http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.28http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.29http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.29http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.29http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.28http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.27http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.26http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.25http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.24http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.23http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.22http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.21http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.20http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.19http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.18http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.17http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.16
  • 7/31/2019 Ch11 Preview Cs

    5/6

    Programming the .NET Compact Framework in C# By Paul Yao & David Durant

    Chapter 11 Storage Page 5Copyright 2003 Paul Yao & David Durant

    platform developer. Uncompressed ROM files run in place, in what is called an XIP (eXecute InPlace) region. An advantage of XIP regions is that ROM images can be mapped directly intovirtual memory, allowing system files to be available without using precious program memory.On the down side, some ROM is slower than RAM, so code running in ROM may executes alittle slower than code that is located elsewhere. [Comment 11.30]

    The \windows folder can also include files from the RAM-based object store. You can copy

    files and even overwrite some of the ROM-based files that are in the \windows folder. Ofcourse, the file system does not actually copy to / overwrite ROM files, but it does note that youhave newer version of a ROM file in RAM, and uses the newer version whenever the file isrequested. [Comment 11.31]

    Another difference between Windows CE-powered systems and desktop systems is thatWindows CE does not support a PATH environment variable that determines the order in whichdirectories are searched when resolving requests to locate programs or shared libraries. Thehard-coded search sequence is as follows: [Comment 11.32]

    The absolute path (when specified) [Comment 11.33]

    The .exe launch directory (when searching for libraries) [Comment 11.34]

    The \windows directory [Comment 11.35]

    ROM DLL files [Comment 11.36]

    While there is no PATH variable, starting with Windows CE 4.1 there is support for a registry-key

    that provides a search path: HKEY_LOCAL_MACHINE\Loader, with the SystemPath property. Thebest resource for details on this subject is the Win32 documentation for Windows CE,specifically the remarks that accompany the Win32 LoadLibrary function. [Comment 11.37]

    RAM based files

    At system boot time, the amount of RAM available for the object store is specified duringplatform creation. It can be modified on some platforms (including the Pocket PC) by a user intheMemory tab of the Control Panel. This change can also be made programmatically through

    the native SetSystemMemoryDivision Windows CE functions. (A companion function,GetSystemMemoryDivision , lets you query the current setting.) These functions are availableto application programs but, as of the writing of this book, are only documented in the platformbuilder. [Comment 11.38]

    Because the object store has a limited size, a compression algorithm is applied to data inthe object store. It is a simple, fast algorithm that achieves approximately 2:1 compression. Themaximum size of a single object store file is 32MB. The maximum size of a CE database, whichare also located in the object store, is 16MG. [Comment 11.39]

    On the Pocket PC, user files are meant to be stored in a directory named My Documents. Thisis the only directory available through the system-provided file-open and file-save dialog boxes,although a user can see the contents of other directories using theFile Explorer application.[Comment 11.40]

    This completes our introduction to storage. You now know that there a unified view of aphysically diverse file system, and that there is a RAM-based object store which contains a filesystem, a registry, and CE databases. The focus for the remainder of this chapter is file I/O andregistry access. [Comment 11.41]

    http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.30http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.30http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.31http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.31http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.32http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.32http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.33http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.33http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.34http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.34http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.35http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.35http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.36http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.36http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.37http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.37http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.38http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.38http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.39http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.39http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.40http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.40http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.40http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.41http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.41http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.41http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.40http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.40http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.39http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.38http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.37http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.36http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.35http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.34http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.33http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.32http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.31http://www.paulyao.com/ReaderFeedback/Feedback.aspx?ID=11CS.30
  • 7/31/2019 Ch11 Preview Cs

    6/6

    Chapter 1, 1

    Thank You

    Thank you for taking the time to read this preview chapter. We hope it has provided youinsights and tips to help with your Compact Framework programming project. You can help us

    create a better book by clicking the comment link at the end of each paragraph, and sending your

    comments and suggestions on our review web site.

    Preview Chapter Text

    Our public review site provides the complete table of contents for the Compact Framework

    book at this link: http://www.paulyao.com/cfbook.htm. That table of contents contains links to all

    the preview chapters. The preview chapter provides the complete outline of topics covered in a

    chapter, and also the first section or two from each chapter.

    Complete Chapter Text

    You can get the complete text for each chapter, available to readers who register at our web

    site. Registration is simply and easy we only ask for an email address. To register, click on this

    link: http://www.paulyao.com/ReaderFeedback/Logon.aspx.

    When you register, you can download the available chapters from the full-text Table of

    Contents, available at this link: http://www.paulyao.com/ReaderFeedback/default.aspx. We notify

    registered readers of new chapters and chapter updates as they become available.

    http://www.paulyao.com/cfbook.htmhttp://www.paulyao.com/cfbook.htmhttp://www.paulyao.com/ReaderFeedback/Logon.aspxhttp://www.paulyao.com/ReaderFeedback/Logon.aspxhttp://www.paulyao.com/ReaderFeedback/default.aspxhttp://www.paulyao.com/ReaderFeedback/default.aspxhttp://www.paulyao.com/ReaderFeedback/default.aspxhttp://www.paulyao.com/ReaderFeedback/Logon.aspxhttp://www.paulyao.com/cfbook.htm