最好的方式来确定两个路径引用相同的文件在C# [英] Best way to determine if two path reference to same file in C#

查看:189
本文介绍了最好的方式来确定两个路径引用相同的文件在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在即将java7目录,有一个<一href=\"http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#isSameFile(java.nio.file.Path,%20java.nio.file.Path)\"相对=nofollow>新的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.

推荐答案

据我可以看到<一个href=\"http://hg.openjdk.java.net/nio/nio/jdk/file/d5e2831c01c4/src/windows/native/sun/nio/ch/FileKey.c\">(1) <一href=\"http://hg.openjdk.java.net/nio/nio/jdk/file/d5e2831c01c4/src/windows/classes/sun/nio/fs/WindowsFileAttributes.java\">(2) <一href=\"http://hg.openjdk.java.net/nio/nio/jdk/file/d5e2831c01c4/src/windows/classes/sun/nio/fs/WindowsPath.java\">(3) <一href=\"http://hg.openjdk.java.net/nio/nio/jdk/file/d5e2831c01c4/src/share/classes/java/nio/file/FileRef.java\">(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包裹着,所以你将不得不使用<一个href=\"http://www.pinvoke.net/default.aspx/kernel32/GetFileInformationByHandle.html\">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).

这篇关于最好的方式来确定两个路径引用相同的文件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆