Why isn't this DirectoryInfo comparison working? [duplicate]
Asked Answered
N

4

10

Possible Duplicate:
How to check whether 2 DirectoryInfo objects are pointing to the same directory?

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?

Nolde answered 1/7, 2010 at 4:9 Comment(3)
What reason of comparing DirectoryInfo objects? Maybe you should compare .FullPath?Leper
Those aren't the same paths. Is this some kind of Stroop test?Alceste
Also, your close-braces are commented out. If you want us to put energy in to helping you, won't you put energy in to asking a good question?Alceste
P
8

I believe you want to do this :

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 
}

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

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

Paginate answered 1/7, 2010 at 4:19 Comment(2)
Bear in mind that the same two paths can have different casing => case insensitive comparison should be used (as in my answer's comments).Worl
Also consider that Windows supports both \ and / as directory path separators, but Path.Combine does not normalize path separators, and that trailing slashes will also cause this code to fail.Ottilie
A
7

I did a Google Search for "DirectoryInfo equality" and found several great results, including one on StackOverflow (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.

Alceste answered 1/7, 2010 at 4:30 Comment(1)
Some ways to "fool" DirectoryInfo: the subst command, the net use command or network share.Worl
H
2

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

Harmonist answered 1/7, 2010 at 10:51 Comment(2)
The problem of the final slash still remains.Jermainejerman
@SerG: indeed. I was more trying to explain the ByVal vs ByRef difference than answering the specific question (and I didn't thought of that case either).Harmonist
W
1

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

Worl answered 1/7, 2010 at 4:20 Comment(2)
Is it only by using the properties, i.e. the .FullName property that you are comparing value rather than instance?Nolde
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.Worl

© 2022 - 2024 — McMap. All rights reserved.