How can I determine that two paths point to the same file?

  • Thread starter Thread starter Jeff Johnson
  • Start date Start date
J

Jeff Johnson

Given two file paths, I would like to determine with certainty whether those
paths refer to the same physical file. Now, there is Path.GetFullPath(), and
that's very helpful. But what if I refer to a file on my machine through a
UNC path? Example:

C:\Temp\123.txt
\\MYCOMP\DRIVE_C\Temp\123.txt

\\MYCOMP\DRIVE_C is a share mapped to C:\, so ultimately both these paths
point to the same physical file. Does the .NET Framework provide any means
of mapping this? Please note that I don't in any way expect to get local
path translation for a REMOTE server, but I think it ought to be available
when a UNC path points to the LOCAL machine. If .NET offers nothing
built-in, does anyone know of a Windows API function that might do this?

I realize that I could enumerate shares and do all the heavy lifting myself;
I was just wondering if this sort of mapping is already written and
available to me.
 
Hello Jeff,
Given two file paths, I would like to determine with certainty whether
those paths refer to the same physical file.
I was just wondering if this sort of mapping is already written and
available to me.

I don't think a ready-made function exists for this, either in .NET or
Windows API. At least in .NET, you would need to enumerate the shares on the
(local) computer, and then check to which paths they point to. Note that on
NTFS, you can also mount disk partitions into directories, i.e. your regular
"E:\" could be mounted to "C:\MoreStuff".

So to make sure your code would work in all situations, your solution would
become quite complex, I'm afraid. However, in case of UNC paths and share on
the local computer, the solution ought to be quite straightforward.

Hope this helps!

--
Regards,

Jani Järvinen
C# MVP
Vantaa, Finland
E-mail: (e-mail address removed)
Business: http://www.nimacon.net/
Personal: http://www.saunalahti.fi/janij/
 
Back
Top