5

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!

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138
  • place a file in the directory and check for its existence? – Benj Jun 28 '15 at 07:36
  • 3
    this checks if the file exists in a folder, not if source and target path are identical – Marco Jun 28 '15 at 07:39
  • 1
    Possibly related: http://stackoverflow.com/questions/410705/best-way-to-determine-if-two-path-reference-to-same-file-in-c-sharp – TyCobb Jun 28 '15 at 07:43
  • Do you consider these two paths identical? – Yuval Itzchakov Jun 28 '15 at 07:54
  • place one unique (guid) file in one and check for it in the other. – TaW Jun 28 '15 at 07:54
  • 1
    @Serv True, but with that you could simulate 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. – fsacer Jun 28 '15 at 07:54
  • 1
    Summary: it's not straight forward. Folders don't have a unique id / guid, they just have a path. As you're approaching the same path via two different shares, there's no way to know. You could create a share directly to Folder3, eg '\\host\Folder3\' - so you can't just remove a prefix. The point of shares is that they don't specify a drive a drive and can be moved without changing the client - eg, D: gets full, so Folder3 is moved to E: - still shared as '\\host\folder3' but now on E:\Somefolder\Folder3. – freedomn-m Jun 28 '15 at 08:04
  • 1
    create `DirectoryInfo` instances for each of the paths and then try comparing the `FullName` not sure but give a try. – Vignesh.N Jun 28 '15 at 08:13
  • I "fixed" my problem by a disgusting way: move the file to a new folder that I created and will know that there is no such folder, and then move it to the destination folder.. It's disgusting but will work.. – Alon Shmiel Jun 28 '15 at 09:51

1 Answers1

2

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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
fsacer
  • 1,382
  • 1
  • 15
  • 23