Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if two paths are the same [duplicate]

Tags:

c#

I have two paths:

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

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

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

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

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

Any help appreciated!

like image 953
Alon Shmiel Avatar asked Oct 31 '22 01:10

Alon Shmiel


1 Answers

You could have a method like this to check if 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 simulates the behaviour of checking if paths are the same you can create a file with unique name and check if it exists in other directory. Then you could delete the file created because it is not needed anymore. This is not the best solutiton, however it may be sufficient.

This also doesn't handle errors that can occur. For error handling look at this: https://msdn.microsoft.com/en-us/library/vstudio/as2f1fez(v=vs.110).aspx

like image 146
fsacer Avatar answered Nov 15 '22 05:11

fsacer