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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With