I understand this is an old post, but all the answers are ultimately based on a textual comparison of the two names. Trying to get two "normalized" names that take into account the many possible ways of referencing the same file object is almost impossible. There are problems such as: connections, symbolic links, shared network files (links to the same file in different manners), etc. Etc.
The question specifically requested that the solution does not require I / O, but if you are going to deal with network paths, you absolutely need to do IO: there are times when it is simply impossible to determine from any local path manipulation whether links to two files refer to the same physical file. (This can be easily understood as follows: suppose that the file server has a connection to the Windows directory somewhere inside a common subtree. In this case, the file can be referenced either directly or through the connection. But the connection is on the file server, and therefore for the client itโs just it is impossible to determine, only through local information, that two reference file names refer to the same physical file: the information is simply not available locally for the client.Thus, it is absolutely necessary to perform a minimum IO - for example, open two file object descriptors - determine whether links refer to the same physical file.)
The following solution performs some I / O, but correctly determines whether two file system links are semantically identical, that is, they refer to the same file object. (if no file specification relates to a valid file object, all bids are disabled):
public static bool AreDirsEqual(string dirName1, string dirName2) {
The idea for this was derived from a response from Warren Stevens in a similar question that I posted on SuperUser: https://superuser.com/a/881966/241981
David I. McIntosh Sep 08 '16 at 19:58 2016-09-08 19:58
source share