Last listened to Symphony No. 4 in G (1993 Digital Remaster): III. Ruhevoll by Paul Kletzki (on 6 Sep 2010, 05:25)

Following on from my last post about browser differences when using a FileUpload control, this post is documenting a small inconsitency I've found, only this time it appears to be with the .NET Framework itself, rather than different browsers using ASP.NET applications.

When you call Path.GetDirectoryName and pass in the path of a file, but for whatever reason your filename is in Windows tilde format, it may return a value with a different case than passing in the filename in full (i.e. without a tilde).

Let's say that you have a folder on the root of your hard drive called "FOLDER" (note that our folder name is in uppercase) and you have a file inside it called "filewithlongname.txt". The full path of that file is obviously "C:\FOLDER\filewithlongname.txt". Take a look at the following, but bear in mind that we'll pass in "C:\folder" (lowercase) as the file path, which is different in case to the real folder on our hard drive:

string dir1 = Path.GetDirectoryName("C:\\test\\Folder\\filewi~1.txt");
string dir2 = Path.GetDirectoryName("C:\\test\\Folder\\filewithlongname.txt");

These are the values you'll get:

// dir1: C:\test\FOLDER
// dir2: C:\test\Folder

So it appears that when you pass in the path of a file to the Path.GetDirectoryName method and your filename is in tilde format, the return value will be in the same case as it is on the hard drive, whereas if you don't use tilde format and instead pass the full name of the file in, it will return a value in the same case as what was passed in to it, regardless of whether that matches what's on the hard drive.

When using an ASP.NET File Upload control (or the HTML equivalent) it's worth remembering that when you come to use the FileName property in your code, it annoyingly has different values depending on what browser you are using.

If you have one of these on your page...

<asp:FileUpload id="FileUpload1" runat="server" /> 

... or the HTML version...

<input id="FileUpload2" runat="server" type="file" />

... when you come to get the name of the user-selected file in your code (which for example was a file picture.jpg in C:\Pics\), you will get the following results depending on the users browser:

Control: FileUpload1 FileUpload1 FileUpload2 FileUpload2
Property: FileName PostedFile.FileName Value PostedFile.FileName
Firefox 2 picture.jpg picture.jpg picture.jpg picture.jpg
Firefox 3 picture.jpg picture.jpg picture.jpg picture.jpg
IE7 picture.jpg C:\Pics\picture.jpg C:\Pics\picture.jpg C:\Pics\picture.jpg
Opera 9 picture.jpg picture.jpg picture.jpg picture.jpg
Safari 3 picture.jpg picture.jpg picture.jpg picture.jpg

As you can see, it is Internet Explorer that is the odd one out. What this means is that you can't rely on using the FileName property without first checking the value and trimming out the unwanted file path. For example, to get picture.jpg no matter what browser is used, you'll have to do something like this:

string file = FileUpload2.PostedFile.FileName;
int slash = file.LastIndexOf("\\");
if (slash > 0) file = file.Substring(slash, file.Length - 1);

Don't let this catch you out.