2010-07-01 30 views
7

可能重複:
How to check whether 2 DirectoryInfo objects are pointing to the same directory?爲什麼不是這個DirectoryInfo比較工作?

var dirUserSelected = new DirectoryInfo(Path.GetDirectoryName("SOME PATH")); 
var dirWorkingFolder = new DirectoryInfo(Path.GetDirectoryName("SAME PATH AS ABOVE")); 

if (dirUserSelected == dirWorkingFolder) 
{ 
    //this is skipped 
} 

if (dirUserSelected.Equals(dirWorkingFolder)) 
{ 
    //this is skipped 
} 

雖然調試,我可以檢查每個值和他們是平等的。所以我猜這是另一個byval byref誤解...請別人,我如何比較這兩件事?

+2

比較DirectoryInfo對象的原因是什麼? 也許你應該比較.FullPath? – griZZZly8 2010-07-01 04:13:03

+0

那些路徑不一樣。這是一種Stroop測試嗎? – 2010-07-01 04:20:03

+2

另外,你的大括號被註釋掉了。如果你希望我們投入精力來幫助你,你不會花精力問一個好問題嗎? – 2010-07-01 04:21:09

回答

9

我相信你想這樣做:

var dirUserSelected = new DirectoryInfo(Path.GetDirectoryName(@"c:\some\path\")); 
var dirWorkingFolder = new DirectoryInfo(Path.GetDirectoryName(@"c:\Some\PATH")); 

if (dirUserSelected.FullName == dirWorkingFolder.FullName) 
{ // this will be skipped, 
    // since the first string contains an ending "\" and the other doesn't 
    // and the casing in the second differs from the first 
} 

// to be sure all things are equal; 
// either build string like this (or strip last char if its a separator) 
// and compare without considering casing (or ToLower when constructing) 
var strA = Path.Combine(dirUserSelected.Parent, dirUserSelected.Name); 
var strB = Path.Combine(dirWorkingFolder.Parent, dirWorkingFolder.Name); 
if (strA.Equals(strB, StringComparison.CurrentCultureIgnoreCase) 
{ //this will not be skipped 
} 

............ 

在你比如你這就是比較2個不同的對象,爲什麼它們不相等。我相信你需要比較Parths,所以使用上面的代碼。

+1

請記住,相同的兩個路徑可以有不同的套管=>應該使用不區分大小寫的比較(如我的答案的評論)。 – 2010-07-01 08:15:38

1

因爲它比較這兩個實例,而不是它們的值(兩個引用)。

+0

它只是通過使用屬性,即比較值而不是實例的'.FullName'屬性嗎? – baron 2010-07-01 04:21:35

+4

在這種情況下,是的。某些類實現了'IEquatable ''接口並且覆蓋'Object.Equals(Object)'來進行相等操作。 'DirectoryInfo'必須實現'Equals(DirectoryInfo di){return String.Compare(this.FullName,di.FullName,true)== 0;上面的代碼工作(除其他外)。改變了比較,因爲路徑可能有不同的大小寫。 – 2010-07-01 04:47:29

4

我做了谷歌搜索「的DirectoryInfo平等」和發現了幾個巨大的成績,其中包括在計算器上(How to check whether 2 DirectoryInfo objects are pointing to the same directory?

如果兩個Directory.FullName的比賽,那麼你就知道他們相同,但如果他們不匹配,你還是不太瞭解。有簡稱和鏈接以及其他許多原因,兩個不同的字符串可能指向磁盤上的相同位置。

如果你依然知道兩個字符串不是相同的位置,並且安全處於危險之中,那麼很可能會造成安全漏洞。仔細踩踏。

+3

「欺騙」DirectoryInfo的一些方法:「subst」命令,「net use」命令或網絡共享。 – 2010-07-01 05:06:55

2

由於雅羅斯拉夫揚德克說(對不起,我不能發表評論,沒有足夠的聲譽)

因爲它比較這兩個 實例,而不是他們的價值(二 引用)。

實際上,對於其他大量的情況也是如此!對於前

IPAddress ip1 = IPAddress.Parse("192.168.0.1"); 
IPAddress ip2 = IPAddress.Parse("192.168.0.1"); 

兩個IP地址代表相同的地址,但你必須兩種截然不同的情況下IPAddress類的。所以當然「ip1 == ip2」和「ip1.Equals(ip2)」都是錯誤的,因爲它們不指向同一個對象。

現在,如果您選中「ip1.Address == ip2.Address」,結果將爲true,因爲IPAddress.Address是「long」,因此您正在比較2個值類型。請注意,即使字符串是引用類型而非值類型(但字符串確實是特殊的),「ip1.ToString()== ip2.ToString()」也是如此。

因此,確實在你的情況下,你想比較FullName屬性(這是一個字符串,所以沒有問題)。

你說

難道只有通過使用屬性,即您要比較值,而不是實例.FullName財產?

實際上,它與屬性是值類型還是引用類型有關。當比較參考類型時,在大多數情況下,比較是否指向同一個對象,但是可以重寫方法或創建操作符,以便在對象上完成比較(同樣也可以像Jaroslav Jandek已經指出)。

HTH

+0

最後的斜線仍然存在。 – SerG 2014-01-28 14:24:09

+0

@SerG:的確如此。我更願意解釋ByVal與ByRef的區別,而不是回答具體問題(而且我也沒有考慮過這種情況)。 – user276648 2014-01-29 00:49:05