15.01.2013 Views

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

AppDomain.CurrentDomain.BaseDirectory returns the application base directory,<br />

which in normal cases is the folder containing the program’s executable. To specify<br />

a filename relative to this directory, you can call Path.Combine:<br />

string baseFolder = AppDomain.CurrentDomain.BaseDirectory;<br />

string logoPath = Path.Combine (baseFolder, "logo.jpg");<br />

Console.WriteLine (File.Exists (logoPath));<br />

You can read and write across a network via a UNC path, such as \\JoesPC\PicShare<br />

\pic.jpg or \\10.1.1.2\PicShare\pic.jpg.<br />

Specifying a FileMode<br />

All of FileStream’s constructors that accept a filename also require a FileMode enum<br />

argument. Figure 14-3 shows how to choose a FileMode, and the choices yield results<br />

akin to calling a static method on the File class.<br />

Figure 14-3. Choosing a FileMode<br />

File.Create and FileMode.Create will throw an exception if<br />

used on hidden files. To overwrite a hidden file, you must delete<br />

and re-create it:<br />

if (File.Exists ("hidden.txt")) File.Delete ("hidden.txt");<br />

Constructing a FileStream with just a filename and FileMode gives you (with just one<br />

exception) a readable writable stream. You can request a downgrade if you also<br />

supply a FileAccess argument:<br />

Using Streams | 545<br />

Streams and I/O

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

Saved successfully!

Ooh no, something went wrong!