tags:

views:

243

answers:

4

I have 2 DirectoryInfo objects and want to check if they are pointing to the same directory. Besides comparing their Fullname, are there any other better ways to do this? Please disregard the case of links.

Here's what I have.

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

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

Thanks.

+3  A: 

Under Linux you could compare the INode numbers of the two files wheather they are identical. But under Windows there is no such concept, at least not that I know off. You would need to use p/invoke to resolve the links if any.

Comparing strings is the best you can do. Note that using String.Compare(str1, str2, StringComparison.InvariantCultureIgnoreCase) is a bit faster than you approach.

codymanix
was there something wrong with my answer?
codymanix
What's wrong with this answered? Why was it marked down? I find the tip of using String.Compare() good.
mqbt
A: 

On Windows Vista I could not create two folders/directories in the same path where the only difference in the names was letter-casing.

Using your code : if I called this :

di1.Create();
di2.Create();

No error would be thrown, but the result would be only one directory named C:\temp

This :

di1.Create();

followed by :

if(di2.Exists) Console.WriteLine("di2 exists");

Would print 'di2 exists' to the console window. Code fragments tested in .NET 3.0 and .NET 2.0

BillW
I'm not trying to create any directories here. Let's say you have a function bool Foo(DirectoryInfo d1, DirectoryInfo d2){}. Foo() returns true if d1 and d2 represent the same directory in the file system.
mqbt
Well, sorry if I missed the point here : if the underlying software "reality" stops you from having a case where two directories could have names that vary only by case : then I can only assume you are dealing with an error condition here : perhaps a user has typed in a directory name wrong. In that case one strategy is to convert all user entered path names into upper case and avoid this kind of comparison in the first place, using the Exists function to validate the file exists as needed.
BillW
A: 

Case-insensitive comparison is the best you can get. Extract it to a helper class just in case mankind comes up with a better method.

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

and use:

if (di1.IsEqualTo(di2))
{
    // Code here
}
Konstantin Spirin
A: 

You can use Uri objects instead. However, your Uri objects must point to a "file" inside these directories. That file doesn't actually have to exist.

    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));

    }

This will print out:

u1 == u2 ? True
u2 == u3 ? True
Matthew Cole