C# Filepath Recasing

前端 未结 6 1524
青春惊慌失措
青春惊慌失措 2020-12-29 06:37

I\'m trying to write a static member function in C# or find one in the .NET Framework that will re-case a file path to what the filesystem specifies.

Example:

<
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 06:58

    I have something more efficient but:

    1) It doesn't seem to work for all cases. (I've not figured out the pattern of which files and directories it correctly gets the casing, and which ones it does not.)

    2) It's Windows specific.

    static string GetProperFilePathCapitalization1(string filename)
    {
        StringBuilder sb = new StringBuilder(260);
        int length = GetLongPathName(filename, sb, sb.Capacity);
    
        if (length > sb.Capacity)
        {
            sb.Capacity = length;
            length = GetLongPathName(filename, sb, sb.Capacity);
        }
    
        if (0 == length)
            throw new Win32Exception("GetLongPathName");
    
        return sb.ToString();
    }
    
    [DllImport("kernel32.dll")]
    static extern int GetLongPathName(string path, StringBuilder pszPath, int cchPath);
    

提交回复
热议问题