2009-11-25 60 views
11

我有2 DirectoryInfo對象,並希望檢查他們是否指向同一目錄。除了比較他們的全名外,還有其他更好的方法來做到這一點嗎?請忽略鏈接的情況。如何檢查2個DirectoryInfo對象是否指向同一個目錄?

這是我的。

DirectoryInfo di1 = new DirectoryInfo(@"c:\temp"); 
DirectoryInfo di2 = new DirectoryInfo(@"C:\TEMP"); 

if (di1.FullName.ToUpperInvariant() == di2.FullName.ToUpperInvariant()) 
{ // they are the same 
    ... 
} 

謝謝。

+0

相關問題:http://stackoverflow.com/q/2281531/1082063 – 2016-10-12 03:39:50

回答

6

在Linux下,您可以比較兩個文件的INode編號是否相同。但在Windows下不存在這樣的概念,至少不是我知道的。您需要使用p/invoke來解析鏈接(如果有)。

比較字符串是你可以做的最好的。請注意,使用String.Compare(str1,str2,StringComparison.InvariantCultureIgnoreCase)比您的方法快一點。

+0

是我的答案有問題嗎? – codymanix 2009-11-25 02:08:00

+0

這有什麼問題呢?爲什麼它被標記?我發現使用String.Compare()的技巧很好。 – tranmq 2009-11-25 02:46:33

+1

這與問題中提供的解決方案基本相同。更好的方法是使用Uri,它將處理不同的格式化等。 – 2013-04-19 15:41:09

0

不區分大小寫的比較是最好的,你可以得到。將它解壓縮到一個輔助類中,以防萬一人類想出更好的方法。

public static class DirectoryInfoExtensions 
{ 
    public static bool IsEqualTo(this DirectoryInfo left, DirectoryInfo right) 
    { 
     return left.FullName.ToUpperInvariant() == right.FullName.ToUpperInvariant(); 
    } 
} 

及用途:

if (di1.IsEqualTo(di2)) 
{ 
    // Code here 
} 
6

您可以使用開放的對象,而不是。但是,您的Uri對象必須指向這些目錄中的「文件」。該文件實際上並不存在。

private void CompareStrings() 
    { 
     string path1 = @"c:\test\rootpath"; 
     string path2 = @"C:\TEST\..\TEST\ROOTPATH"; 
     string path3 = @"C:\TeSt\RoOtPaTh\"; 

     string file1 = Path.Combine(path1, "log.txt"); 
     string file2 = Path.Combine(path2, "log.txt"); 
     string file3 = Path.Combine(path3, "log.txt"); 

     Uri u1 = new Uri(file1); 
     Uri u2 = new Uri(file2); 
     Uri u3 = new Uri(file3); 

     Trace.WriteLine(string.Format("u1 == u2 ? {0}", u1 == u2)); 
     Trace.WriteLine(string.Format("u2 == u3 ? {0}", u2 == u3)); 

    } 

這將打印出:

u1 == u2 ? True 
u2 == u3 ? True 
+0

這實際上應該是答案,因爲它涵蓋邊緣 - 例如「\ ..\「,雖然它需要一個小小的解決方法 – 2017-03-03 11:46:21

+1

,但有一個問題,它會將諸如%51之類的內容翻譯爲字母而不是將它們留下,因此如果嘗試此路徑: '@」c:\ test \ rootQpath 「 @」C:\ TEST \ .. \ TEST \ ROOT%51PATH「' 它會返回true – 2017-03-03 12:02:17

+0

雖然'C:\'會工作,如果路徑的形式是C,則會拋出'UriFormatException' :'(省略'\')作爲'Path.Combine'會導致類似這樣的'C:log.txt' – Petaflop 2018-01-22 12:20:42

2

here啓發:

static public bool SameDirectory(string path1, string path2) 
{ 
    return (
     0 == String.Compare(
      System.IO.Path.GetFullPath(path1).TrimEnd('\\'), 
      System.IO.Path.GetFullPath(path2).TrimEnd('\\'), 
      StringComparison.InvariantCultureIgnoreCase)) 
     ; 
}  

工程文件太...

(BTW理論上的問題是重複的,但這種是原來的,另一個是最接近的答案......)

HTH

0

一些擴展方法,我寫了一個最近的項目包括一個會做它:

public static bool IsSame(this DirectoryInfo that, DirectoryInfo other) 
    { 
     // zip extension wouldn't work here because it truncates the longer 
     // enumerable, resulting in false positive 

     var e1 = that.EnumeratePathDirectories().GetEnumerator(); 
     var e2 = other.EnumeratePathDirectories().GetEnumerator(); 

     while (true) 
     { 
      var m1 = e1.MoveNext(); 
      var m2 = e2.MoveNext(); 
      if (m1 != m2) return false; // not same length 
      if (!m1) return true; // finished enumerating with no differences found 

      if (!e1.Current.Name.Trim().Equals(e2.Current.Name.Trim(), StringComparison.InvariantCultureIgnoreCase)) 
       return false; // current folder in paths differ 
     } 
    } 

    public static IEnumerable<DirectoryInfo> EnumeratePathDirectories(this DirectoryInfo di) 
    { 
     var stack = new Stack<DirectoryInfo>(); 

     DirectoryInfo current = di; 

     while (current != null) 
     { 
      stack.Push(current); 
      current = current.Parent; 
     } 

     return stack; 
    } 

    // irrelevant for this question, but still useful: 

    public static bool IsSame(this FileInfo that, FileInfo other) 
    { 
     return that.Name.Trim().Equals(other.Name.Trim(), StringComparison.InvariantCultureIgnoreCase) && 
       that.Directory.IsSame(other.Directory); 
    } 

    public static IEnumerable<DirectoryInfo> EnumeratePathDirectories(this FileInfo fi) 
    { 
     return fi.Directory.EnumeratePathDirectories(); 
    } 

    public static bool StartsWith(this FileInfo fi, DirectoryInfo directory) 
    { 
     return fi.Directory.StartsWith(directory); 
    } 

    public static bool StartsWith(this DirectoryInfo di, DirectoryInfo directory) 
    { 
     return di.EnumeratePathDirectories().Any(d => d.IsSame(directory)); 
    } 
相關問題