如何确定多个文件路径格式指向同一物理位置

Gen*_*neS 10 c# directory path

可能重复:
确定两个路径引用C#中同一文件的最佳方法

有几种方法可以指定目录位置.

例如:
\\ machineName\c $\rootPath\subPath
\\ machineName\shareName (指向subPath的共享)
C:\ rootPath\subPath
subPath(相对路径,如果已经在C:\ rootPath中)

我需要确定所有这些路径彼此"相等"(实际上是硬盘驱动器上的相同物理位置).

有什么方法可以在C#中做到这一点吗?

El *_*oco 5

正如Oded所说,在.Net中这很棘手.您可以通过将具有长随机生成的文件名的文件写入该位置来欺骗(取决于您的确切要求和权限等),然后查看您是否可以从其他位置看到它.一点点黑客,但这是一个非常合理的测试,我认为,而不是依赖于解析映射驱动器等.

很多人为VB道歉 - 这就是我在这款小型上网本上的所有内容...... C#不会太差异......

用法,例如

If sameLocation("\\machineName\c$\rootPath\subPath","\\machineName\shareName") Then...

Public Function sameLocation(ByVal sPath1 As String, ByVal sPath2 As String) As TriState
    Dim sFile As String = randomFilename()
    Dim sFullPath1 As String = sPath1 & "\" & sFile
    Dim sFullPath2 As String = sPath2 & "\" & sFile
    Dim bReturn As Boolean = False
    Try
        Dim fs As New FileStream(sFullPath1, FileMode.CreateNew)
        fs.Close()
    Catch ex As Exception
        Return TriState.UseDefault
    End Try

    Try
        bReturn = File.Exists(sFullPath2)
    Catch ex As Exception
        Return TriState.UseDefault
    End Try

    File.Delete(sFullPath1)
    Return bReturn
End Function

Public Function randomFilename() As String
    Dim r As New Random
    Randomize(My.Computer.Clock.TickCount)
    Dim sb As New StringBuilder
    Dim chars As Int16 = 100
    While chars > 0
        chars -= 1
        sb.Append(Chr(r.Next(26) + 65))
    End While
    Return sb.ToString
End Function
Run Code Online (Sandbox Code Playgroud)

您可以添加更多安全性,即读取时间戳等...