The other night we were performing tests of the Digipede Network on .NET 2.0 Beta 2 and came across an unexpected failure. The failing code instantiated a FileInfo object with the fileName argument of a nonexistent file. Then the code read the Attributes property. This resulted in the following exception / stack trace:
Unhandled Exception: System.IO.FileNotFoundException: Could not find file 'xxxxx'. File name: 'xxxxx' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath at System.IO.FileSystemInfo.get_Attributes()
The code was supposed to read Attributes only if Exists was true. This was clearly a logic error, but my first question was: “How does this ever work?” As I didn’t have time to look into it further, I just went ahead and fixed the problem.
Today I tested this code again in .NET 1.1 and found that Attributes returns a default value (all bits on) even when the file cannot be found. I looked this up in the BreakingChanges.chm (that hasn’t been updated since May) and found no reference to this change in behavior.
I decided to test it again on .NET 2.0 Beta 2 (v2.0.50215) and confirmed again that I get the above exception. This appears to be an unintentional breaking change to Beta 2.
Then I tried it with the .NET 2.0 RC (v2.0.50727, the one from the Visual Studio 2005 RC that was handed out at PDC05). This time it worked just like it did in .NET 1.1.
So, the problem came and went.
I figure that the Beta 2 behavior was reasonable; however, the CLR team apparently (and, I think, correctly) prefers compatibility.
Here is the code sample I used to test this out:
using System; using System.IO; namespace FileInfoTest { ////// Summary description for Class1. /// internal class FileInfoTest { ////// The main entry point for the application. /// [STAThread] private static void Main(string[] args) { // check arg length if (args.Length != 1) { Console.WriteLine("Usage: FileInfoTest file"); return; } // get FileInfo FileInfo fileInfo = new FileInfo(args[0]); Console.WriteLine("Exists:\t{0}", fileInfo.Exists); Console.WriteLine("Directory:\t{0}", fileInfo.DirectoryName); Console.WriteLine("Name:\t{0}", fileInfo.Name); Console.WriteLine("Attributes:\t{0}", fileInfo.Attributes); Console.WriteLine("LastAccessTime:\t{0}", fileInfo.LastAccessTime); Console.WriteLine("LastWriteTime:\t{0}", fileInfo.LastWriteTime); } } }