最好的方式来确定两个路径引用相同的文件在C#

最好的方式来确定两个路径引用相同的文件在C#

问题描述:

在即将java7目录,有一个新的API 检查两个文件对象是同一个文件的参考。

In the upcoming Java7, there is a new API to check if two file object are same file reference.

有没有在.NET框架提供了类似的API?

Are there similar API provided in the .NET framework?

我搜索了过来MSDN但没有什么赐教。

I've search it over MSDN but nothing enlighten me.

我希望它简单,但我不希望文件名,这将导致硬/符号链接,不同风格的路径问题比较。 (?例如 \\\\。\\ C:\\ C:\\ )。

I want it simple but I don't want to compare by filename which will cause problems with hard/symbolic links and different style of path. (e.g. \\?\C:\, C:\).

我要做的仅仅是prevent复制的文件被拖放下降到链表我

What I going to do is just prevent duplicated file being drag and dropped to my linklist.

据我可以看到(1) (2) (3) (4), JDK7做它的方法是调用 GetFileInformationByHandle 上的文件和比较dwVolumeSerialNumber,nFileIndexHigh和nFileIndexLow。

As far as I can see (1) (2) (3) (4), the way JDK7 does it, is by calling GetFileInformationByHandle on the files and comparing dwVolumeSerialNumber, nFileIndexHigh and nFileIndexLow.

每MSDN:

可以比较VolumeSerialNumber和FileIndex成员在BY_HANDLE_FILE_INFORMATION结构中返回以确定是否两个路径映射到相同的目标;例如,你可以比较两个文件路径和确定它们是否映射到同一目录下。

You can compare the VolumeSerialNumber and FileIndex members returned in the BY_HANDLE_FILE_INFORMATION structure to determine if two paths map to the same target; for example, you can compare two file paths and determine if they map to the same directory.

我不认为这个功能是由.NET包裹着,所以你将不得不使用P/Invoke.

I do not think this function is wrapped by .NET, so you will have to use P/Invoke.

这可能会或可能不会进行网络文件。根据MSDN:

It might or might not work for network files. According to MSDN:

取决于操作系统的底层网络元件,并连接到,所述GetFileInformationByHandle函数可能会失败,返回部分信息,或全部信息为给定的文件。服务器的类型

Depending on the underlying network components of the operating system and the type of server connected to, the GetFileInformationByHandle function may fail, return partial information, or full information for the given file.

一个快速测试表明,它按预期工作(相同的值)与Linux系统上的符号链接使用SMB / Samba的连接,但它不能检测到一个文件是相同的使用指向不同的股票进行访问时,相同的文件(FileIndex是相同的,但VolumeSerialNumber不同)。

A quick test shows that it works as expected (same values) with a symbolic link on a Linux system connected using SMB/Samba, but that it cannot detect that a file is the same when accessed using different shares that point to the same file (FileIndex is the same, but VolumeSerialNumber differs).