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.

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

(continued)<br />

ProPerTy<br />

LastAccessTime<br />

LastWriteTime<br />

Name<br />

Root (DirectoryInfo only)<br />

Length (FileInfo only)<br />

desCriPTion<br />

Time the fi le or folder was last accessed<br />

Time the fi le or folder was last modifi ed<br />

Name of the fi le or folder<br />

The root portion of the path<br />

The size of the fi le in bytes<br />

You can also perform actions on the fi le system object using the methods in the following table.<br />

meThod<br />

Create()<br />

Delete()<br />

MoveTo()<br />

CopyTo()<br />

GetDirectories()<br />

GetFiles()<br />

GetFileSystemInfos()<br />

PurPose<br />

Creates a folder or empty fi le of the given name. For a FileInfo this also<br />

returns a stream object to let you write to the fi le. (Streams are covered later in<br />

this chapter.)<br />

Deletes the fi le or folder. For folders, there is an option for the Delete to be<br />

recursive.<br />

Moves <strong>and</strong>/or renames the fi le or folder.<br />

(FileInfo only) Copies the fi le. Note that there is no copy method for folders. If<br />

you are copying complete directory trees you will need to individually copy each<br />

fi le a n d c r e a te n e w f o ld e r s co r r e s p o n d in g to t h e o ld f o ld e r s .<br />

(DirectoryInfo only) Returns an array of DirectoryInfo objects<br />

representing all folders contained in this folder.<br />

(DirectoryInfo only) Returns an array of FileInfo objects representing all<br />

fi le s co n t a in e d in t h is f o ld e r.<br />

(DirectoryInfo only) Returns FileInfo <strong>and</strong> DirectoryInfo objects<br />

representing all objects contained in this folder, as an array of FileSystemInfo<br />

references.<br />

Note that these tables list the main properties <strong>and</strong> methods <strong>and</strong> are not intended to be exhaustive.<br />

The preceding tables do not list most of the properties or methods that allow you to<br />

write to or read the data in fi les. This is actually done using stream objects, which are<br />

covered later in this chapter. FileInfo also implements a number of methods — Open(),<br />

OpenRead(), OpenText(), OpenWrite(), Create(), <strong>and</strong> CreateText() — that return<br />

stream objects for this purpose.<br />

Interestingly, the creation time, last access time, <strong>and</strong> last write time are all writable:<br />

// displays the creation time of a file,<br />

// then changes it <strong>and</strong> displays it again<br />

FileInfo test = new FileInfo(@"C:\MyFile.txt");<br />

Console.WriteLine(test.Exists.ToString());<br />

Console.WriteLine(test.CreationTime.ToString());<br />

test.CreationTime = new DateTime(2010, 1, 1, 7, 30, 0);<br />

Console.WriteLine(test.CreationTime.ToString());<br />

Running this application produces results similar to the following:<br />

True<br />

2/5/2009 2:59:32 PM<br />

1/1/2010 7:30:00 AM<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!