I seems that since NTFS is case insensitive it will always except your input correctly regardless if the name is cased right.
The only way to get the correct path name seems to find the file like John Sibly suggested.
I created a method that will take a path (folder or file) and return the correctly cased version of it: (for the entire path)
public static string GetExactPathName(string pathName)
{
if (!(File.Exists(pathName) || Directory.Exists(pathName)))
return pathName;
var di = new DirectoryInfo(pathName);
if (di.Parent != null) {
return Path.Combine(
GetExactPathName(di.Parent.FullName),
di.Parent.GetFileSystemInfos(di.Name)[0].Name);
} else {
return di.Name.ToUpper();
}
}
Here are some test cases that worked on my machine:
static void Main(string[] args)
{
string file1 = @"c:\documents and settings\administrator\ntuser.dat";
string file2 = @"c:\pagefile.sys";
string file3 = @"c:\windows\system32\cmd.exe";
string file4 = @"c:\program files\common files";
string file5 = @"ddd";
Console.WriteLine(GetExactPathName(file1));
Console.WriteLine(GetExactPathName(file2));
Console.WriteLine(GetExactPathName(file3));
Console.WriteLine(GetExactPathName(file4));
Console.WriteLine(GetExactPathName(file5));
Console.ReadLine();
}
The method will return the supplied value if the file does not exists.
There might be faster methods (this uses recursion) but I'm not sure if there are any obvious ways to do it.