A group blog from members of the VB team
I once visited an ancient tower in China. A sign said "tread carefully for you bear the weight of history on your shoulders". Our guide explained that the tower was over 800 years old! Oh yes, he said, it was built 800 years ago, had burnt to the ground three times in its history, had been moved to completely different locations twice, but it still counted as 800 years old.
Back in VB land I was making two different calls into some framework, and each call gave me back a filename, and I needed to judge whether the two results were talking about the same file. But how? The framework docs didn't give any guarantee that they'd give back filenames in the same format. What if one call returned long filenames and the other returned it with different casing? or 8.3 format? Or UNC? Or long UNC?
The following function is the best I could come up with. I think it only makes sense to compare filenames if they point to an existing file. And I called the function "DidFileNamesPointToSameFile", in the past tense, because by the time the function returns then they might no longer point to the same file anymore!
If anyone has suggestions for neater ways to accomplish the same task, I'd love to hear.
Module
End
PingBack from http://www.easycoded.com/to-compare-two-filenames-lucian-wischik/
Although this will work "most of the time", it is impossible in Windows (and probably other OSes) to make 100% sure two paths or two handles point to the same file, because everything can be virtual, redirected, or whatever by file system drivers, anti viruses, virtual disks, etc... So it really depends on what you really do with this kind of functions, but you're working for NASA or DOD, beware :-)
Excellent (useful) algorithm. It could be a method of System.IO.Path (or File, if more convinient) class :-)
You've been kicked (a good thing) - Trackback from DotNetKicks.com
If you're looking for a solution that allows you to find the "canonical" form of the name (for instance to put the names into a hash table or something), there's an example of that in Writing Secure Code (2nd ed.) page 386-390. Basically it prepends "\\?\" and calls GetFullPathName followed by GetLongPathName. It also rejects out of hand paths that are longer than MAX_PATH or that contain invalid characters (this is a security book after all, so it prefers to jsut call a.text::$DATA invalid than try to handle it).
Anyhow, it's an interesting approach that I wish was wrapped in an API somewhere.
The documentation for GetFileInformationByHandle says: "nFileIndexLow: Low-order part of a unique identifier that is associated with a file. This value is useful ONLY WHILE THE FILE IS OPEN by at least one process. If no processes have it open, the index may change the next time the file is opened."