12.4 Using the FileInfo Class to Display File Information

 <  Day Day Up  >  

12.4 Using the FileInfo Class to Display File Information

You want to determine various attributes of an existing file.


Technique

The FileInfo class contains properties that allow you to retrieve information about an existing file. One of them is the attributes of a file. The Attributes property returns a FileAttributes enumerated data type specifying whether the file is read only, archived, hidden, and many others. To determine whether a file is read only, bitwise AND ( & ) the result of calling GetAttributes with the FileAttributes.ReadOnly value. If the result is greater than 0, the file is read only:

 
 FileInfo fileInfo = new FileInfo( filePath ); if( (fileInfo.Attributes & FileAttributes.ReadOnly) > 0 )     MessageBox.Show( "File is read only" ); 

You can also retrieve file access times. They include the properties CreationTime , which is a DateTime object corresponding to the time the file was created; LastAccessTime , corresponding to the last time the file was accessed either for reading or writing; and LastWriteTime , which represents the last time data was written to the file.

A few path -location properties let you retrieve information about the file as it relates to the local file system. For instance, to retrieve the current directory name of the file, access the DirectoryName property. If you want to actually manipulate that directory, then retrieve the corresponding Directory object by accessing the Directory property. Finally, to retrieve the filename of the file, you can use the Name property, which returns the name of the file; the FullName property, which returns the full path and filename of the file; or the Extension property, which retrieves the file's extension.

Comments

The FileInfo class shares a lot of similarities with the File class, but the differences are enough to warrant a section of its own. The major difference between the two classes is that FileInfo methods are all instance methods , whereas the File class methods are all static. Having all instance methods can be beneficial if you plan to reuse the same object over and over within your class. For instance, if you were using a File class to work with a file, you would have to keep a copy of the file path around to access such things as file attributes. Once you create the FileInfo object, however, all the relevant information is contained within the object and can be retrieved at any time. If your application only occasionally needs to perform file I/O, then using either one of these classes isn't any better than the other. In most instances, the method signatures and functionality are the same, although the FileInfo methods don't need the mandatory string value denoting the location of the file you want to operate on, as is the case with the File class.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net