views:

71

answers:

5
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 
}

Whilst debugging, I can examine the values in each and they ARE equal. So i'm guessing this is another byval byref misunderstanding... Please someone, how do I compare these two things?

+3  A: 

I believe you want to do this :

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

if (dirUserSelected.FullName  == dirWorkingFolder.FullName )
{ //this will not be skipped }

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

In you example you are comparing 2 different objects thats why they are not equal. I believe you need to compare Parths so use the code above.

Incognito
Bear in mind that the same two paths can have different casing => case insensitive comparison should be used (as in my answer's comments).
Jaroslav Jandek
+1  A: 

Because it compares those two instances, not their value (two references).

Jaroslav Jandek
Is it only by using the properties, i.e. the `.FullName` property that you are comparing value rather than instance?
baron
In this case, yes. Some classes implement the `IEquatable<T>` interface and override `Object.Equals(Object)` for the equality operation. The `DirectoryInfo` would have to have implemented `Equals(DirectoryInfo di) { return String.Compare(this.FullName, di.FullName, true) == 0; }` for the code above to work (among other things). Changed the comparison because the paths might have different casing.
Jaroslav Jandek
A: 

You can check this link: http://msdn.microsoft.com/en-us/library/bb546137.aspx

Chinjoo
That's about comparing the contents of two different directories.
Jay Bazuzi
+3  A: 

I did a Google Search for "DirectoryInfo equality" and found several great results, including one on StackOverflow (http://stackoverflow.com/questions/1794025/how-to-check-whether-2-directoryinfo-objects-are-pointing-to-the-same-directory)

If two Directory.FullNames match, then you know they are the same, but if they don't match, you still don't know much. There are short names and links and junctions and many other reasons two different strings could refer to the same location on disk.

If you count on knowing for sure that two strings aren't the same location, and security is at stake, you're likely creating a security bug. Tread carefully.

Jay Bazuzi
Some ways to "fool" DirectoryInfo: the `subst` command, the `net use` command or network share.
Jaroslav Jandek
+1  A: 

As Jaroslav Jandek says (sorry I can't comment, not enough reputation)

Because it compares those two instances, not their value (two references).

And actually it's the same for tons of other cases! For ex

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

Both IP addresses represent the same address, but you have two distinct instances of the IPAddress class. So of course "ip1 == ip2" and "ip1.Equals(ip2)" are both false, because they don't point to the same object.

Now if you check "ip1.Address == ip2.Address" the result will be true as IPAddress.Address is a "long", so you're comparing 2 value types. Note that "ip1.ToString() == ip2.ToString()" will also be true even if a string is a reference type not a value type (but strings are really specials).

So indeed in your case you want to compare the FullName property (it's a string so no problem).

You say

Is it only by using the properties, i.e. the .FullName property that you are comparing value rather than instance?

Actually it has more to do with whether the property is a value type or a reference type. Also when comparing reference types, in most cases the comparison will be whether or not they point to the same object, but it's possible to override methods or create operators so that the comparison is done on the content of the objects (again just like Jaroslav Jandek already pointed out).

HTH