15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

800 ❘ ChaPTer 29 mAnipulAtinG files And the reGistry<br />

{<br />

class Program<br />

{<br />

static void Main(string[] args)<br />

{<br />

using (var mmFile= MemoryMappedFile.CreateFromFile(<br />

@”C:\Users\Bill\Document\Visual Studio 10\Projects\<br />

MappedMemoryFiles\MappedMemoryFiles\TextFile1.txt”,<br />

System.IO.FileMode.Create, “fileH<strong>and</strong>le”, 1024 * 1024))<br />

{<br />

string valueToWrite = "Written to the mapped-memory file on " +<br />

DateTime.Now.ToString();<br />

var myAccessor = mmFile.CreateViewAccessor();<br />

myAccessor.WriteArray(0,<br />

Encoding.ASCII.GetBytes(valueToWrite), 0,<br />

valueToWrite.Length);<br />

var readOut = new byte[valueToWrite.Length];<br />

myAccessor.ReadArray(0, readOut, 0, readOut.Length);<br />

var finalValue = Encoding.ASCII.GetString(readOut);<br />

}<br />

}<br />

}<br />

}<br />

Console.WriteLine("Message: " + finalValue);<br />

Console.ReadLine();<br />

code download MappedMemoryFiles.sln<br />

In this case, a mapped-memory file is created from a physical file using the CreateFromFile() method. In<br />

addition to a mapped-memory file, you then need to create an accessor object to this mapping. That is done<br />

using the following:<br />

var myAccessor = mmFile.CreateViewAccessor();<br />

After the accessor is in place, you can then write or read to this mapped-memory location as shown in the<br />

code example.<br />

It is also possible to create multiple accessors to the same mapped-memory location as shown here:<br />

var myAccessor1 = mmFile.CreateViewAccessor();<br />

var myAccessor2 = mmFile.CreateViewAccessor();<br />

reading driVe informaTion<br />

In addition to working with files <strong>and</strong> directories, the .<strong>NET</strong> Framework<br />

includes the ability to read information from a specified drive. This is<br />

done using the DriveInfo class. The DriveInfo class can perform a<br />

scan of a system to provide a list of available drives <strong>and</strong> then can dig in<br />

deeper, providing you with tons of details about any of the drives.<br />

For an example of using the DriveInfo class, create a simple Windows<br />

Form that will list all the available drives on a computer <strong>and</strong> then<br />

provide details on a user-selected drive. Your Windows Form will<br />

consist of a simple ListBox <strong>and</strong> should look like Figure 29-13.<br />

When you have the form all set, the code consists of two events — one<br />

for when the form loads <strong>and</strong> another for when the end user makes a<br />

drive selection in the list box. The code for this form is shown here: figure 29-13<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!