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

后端 未结 6 401
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 17:31

I want to do exactly the same as in this question:

Windows file system is case insensitive. How, given a file/folder name (e.g. \"somefile\"), I get t

6条回答
  •  攒了一身酷
    2020-12-05 17:44

    It looks like the best way is to iterate through all folders in the path and get their proper caps:

     Public Function gfnProperPath(ByVal sPath As String) As String
        If Not IO.File.Exists(sPath) AndAlso Not IO.Directory.Exists(sPath) Then Return sPath
        Dim sarSplitPath() As String = sPath.Split("\")
        Dim sAddPath As String = sarSplitPath(0).ToUpper & "\"
        For i = 1 To sarSplitPath.Length - 1
            sPath = sAddPath & "\" & sarSplitPath(i)
            If IO.File.Exists(sPath) Then
                Return IO.Directory.GetFiles(sAddPath, sarSplitPath(i), IO.SearchOption.TopDirectoryOnly)(0)
            ElseIf IO.Directory.Exists(sPath) Then
                sAddPath = IO.Directory.GetDirectories(sAddPath, sarSplitPath(i), IO.SearchOption.TopDirectoryOnly)(0)
            End If
        Next
        Return sPath
    End Function
    

提交回复
热议问题