Detect if two paths are the same

I have two ways:

\\10.11.11.130\FileServer\Folder2\Folder3\ \\10.11.11.130\d$\Main\FileServer\Folder2\Folder3\ 

And I want to determine if both paths are the same.

I want because I'm trying to move one file to another directory. Thus, an exception is thrown for the above paths.

I know I can use try and catch, but is there another way?

I thought about removing d$\Main from the second path, and then compare, but this is not always true.

Any help appreciated!

+5
source share
1 answer

You may have a method to check if this is equal:

 public static bool PathsSame(string pth1, string pth2) { string fName = System.IO.Path.GetRandomFileName(); string fPath = System.IO.Path.Combine(pth1, fName); System.IO.File.Create(fPath); string nPath = System.IO.Path.Combine(pth2, fName); bool same = File.Exists(nPath); System.IO.File.Delete(fPath); return same; } 

This models the behavior of checking whether the paths are the same, you can create a file with a unique name and check if it exists in another directory. Then you can delete the created file because it is no longer needed. This is not the best solutiton, but that may be enough.

It also does not handle errors that may occur. To handle errors, consider the following: https://msdn.microsoft.com/en-us/library/vstudio/as2f1fez(v=vs.110).aspx

+1
source

All Articles