beta.blog

Programming

C# – Compare Equality of two Paths

by on Mar.25, 2013, under Programming

This is an approach how I’d find out whether or not two directory paths are equal.

Case sensitive approach:

if (System.AppDomain.CurrentDomain.BaseDirectory.Replace("/", "\\").TrimEnd(new char[] { '\\' }).Equals(c.InstallationPath.Replace("/", "\\").TrimEnd(new char[] { '\\' })))
{
    // Paths are equal
}

Case insensitive approach:

if (System.AppDomain.CurrentDomain.BaseDirectory.Replace("/", "\\").TrimEnd(new char[] { '\\' }).Equals(c.InstallationPath.Replace("/", "\\").TrimEnd(new char[] { '\\' }), StringComparison.CurrentCultureIgnoreCase))
{
    // Paths are equal
}

It’s for sure not the most elegant way to do this but it works.

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!