C# Filepath Recasing

前端 未结 6 1225
青春惊慌失措
青春惊慌失措 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:50

    The below works fine to the extent I tested... only catch is that the API used is available only in Vista.

    static void Main(string[] args)
    {
        using (FileStream fs = File.OpenRead(@"D:\temp\case\mytest.txt"))
        {
            StringBuilder path = new StringBuilder(512);
            GetFinalPathNameByHandle(fs.SafeFileHandle.DangerousGetHandle(), path, path.Capacity, 0);
            Console.WriteLine(path.ToString());
        }
    }
    
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags);
    

提交回复
热议问题