Getting actual file name (with proper casing) on Windows with .NET

I liked Yona's answer, but I wanted it to:

  • Support UNC paths
  • Tell me if the path didn't exist
  • Use iteration instead of recursion (since it only used tail recursion)
  • Minimize the number of calls to Path.Combine (to minimize string concatenations).
/// <summary>
/// Gets the exact case used on the file system for an existing file or directory.
/// </summary>
/// <param name="path">A relative or absolute path.</param>
/// <param name="exactPath">The full path using the correct case if the path exists.  Otherwise, null.</param>
/// <returns>True if the exact path was found.  False otherwise.</returns>
/// <remarks>
/// This supports drive-lettered paths and UNC paths, but a UNC root
/// will be returned in title case (e.g., \\Server\Share).
/// </remarks>
public static bool TryGetExactPath(string path, out string exactPath)
{
    bool result = false;
    exactPath = null;

    // DirectoryInfo accepts either a file path or a directory path, and most of its properties work for either.
    // However, its Exists property only works for a directory path.
    DirectoryInfo directory = new DirectoryInfo(path);
    if (File.Exists(path) || directory.Exists)
    {
        List<string> parts = new List<string>();

        DirectoryInfo parentDirectory = directory.Parent;
        while (parentDirectory != null)
        {
            FileSystemInfo entry = parentDirectory.EnumerateFileSystemInfos(directory.Name).First();
            parts.Add(entry.Name);

            directory = parentDirectory;
            parentDirectory = directory.Parent;
        }

        // Handle the root part (i.e., drive letter or UNC \\server\share).
        string root = directory.FullName;
        if (root.Contains(':'))
        {
            root = root.ToUpper();
        }
        else
        {
            string[] rootParts = root.Split('\\');
            root = string.Join("\\", rootParts.Select(part => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(part)));
        }

        parts.Add(root);
        parts.Reverse();
        exactPath = Path.Combine(parts.ToArray());
        result = true;
    }

    return result;
}

For UNC paths, this cases the root (\\Server\Share) in title case rather than exact case because it would be a lot more work to try determine the remote server's exact case name and the share's exact case name. If you're interested in adding that support you'll have to P/Invoke methods like NetServerEnum and NetShareEnum. But those can be slow, and they don't support up-front filtering to just the server and share names you're concerned with.

Here's a unit test method for TryGetExactPath (using Visual Studio Testing Extensions):

[TestMethod]
public void TryGetExactPathNameTest()
{
    string machineName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Environment.MachineName.ToLower());
    string[] testPaths = new[]
        {
            @"C:\Users\Public\desktop.ini",
            @"C:\pagefile.sys",
            @"C:\Windows\System32\cmd.exe",
            @"C:\Users\Default\NTUSER.DAT",
            @"C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies",
            @"C:\Program Files (x86)",
            @"Does not exist",
            @"\\Nas\Main\Setups",
            @"\\Nas\Main\Setups\Microsoft\Visual Studio\VS 2015\vssdk_full.exe",
            @"\\" + machineName + @"\C$\Windows\System32\ActionCenter.dll",
            @"..",
        };
    Dictionary<string, string> expectedExactPaths = new Dictionary<string, string>()
        {
            { @"..", Path.GetDirectoryName(Environment.CurrentDirectory) },
        };

    foreach (string testPath in testPaths)
    {
        string lowercasePath = testPath.ToLower();
        bool expected = File.Exists(lowercasePath) || Directory.Exists(lowercasePath);
        string exactPath;
        bool actual = FileUtility.TryGetExactPath(lowercasePath, out exactPath);
        actual.ShouldEqual(expected);
        if (actual)
        {
            string expectedExactPath;
            if (expectedExactPaths.TryGetValue(testPath, out expectedExactPath))
            {
                exactPath.ShouldEqual(expectedExactPath);
            }
            else
            {
                exactPath.ShouldEqual(testPath);
            }
        }
        else
        {
            exactPath.ShouldBeNull();
        }
    }
}

Inspired by Ivan's answer, here is a method that also handles drive letter casing as well:

public string FixFilePathCasing(string filePath)
{
    string fullFilePath = Path.GetFullPath(filePath);

    string fixedPath = "";
    foreach(string token in fullFilePath.Split('\\'))
    {
        //first token should be drive token
        if(fixedPath == "")
        {
            //fix drive casing
            string drive = string.Concat(token, "\\");
            drive = DriveInfo.GetDrives()
                .First(driveInfo => driveInfo.Name.Equals(drive, StringComparison.OrdinalIgnoreCase)).Name;

            fixedPath = drive;
        }
        else
        {
            fixedPath = Directory.GetFileSystemEntries(fixedPath, token).First();
        }
    }

    return fixedPath;
}

I seems that since NTFS is case insensitive it will always accept 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.

Tags:

Windows

.Net