Class of the Day: System.IO.FileInfo

Today I learned about a class in the .NET framework that I've never used: System.IO.FileInfo. I came across it while looking up the appropriate method to determine the size of a file for a unit test. I had expected that there would be a method in the System.IO.File class for this, but I was mistaken. It turns out the proper way is to use the Length property of a FileInfo instance.

FileInfo is kind of a weird class. In fact, I'm not really sure what the design impetus behind it was. It actually duplicates a lot of the behaviour of the File class, which I use all the time. In fact, it duplicates so much so that I would have expected the two to be a single class. It appears that the distinction is primarily that File is static while FileInfo is the instance equivalent, with some of the methods moved to properties. Although it's still not clear to me why they couldn't just have it all in one class, with methods overloaded for static and instance calls.

By way of summary, the FileInfo class inherits from FileSystemInfo. Some of the interesting properties include Directory, Exists, and Length, as well as both "regular" and UTC variants of properties for file access and creation time. Interesting methods include Encrypt() and Decrypt(), CopyTo() and MoveTo(), and SetAccessControl to set ACLs on the file. I wonder how Mono implements that last one. It's supported, but undocumented.

You can reply to this entry by leaving a comment below. You can send TrackBack pings to this URL. This entry accepts Pingbacks from other blogs. You can follow comments on this entry by subscribing to the RSS feed.

Add your comments #

A comment body is required. No HTML code allowed. URLs starting with http:// or ftp:// will be automatically converted to hyperlinks.