在C#中访问文件超出MAX_PATH /。NET [英] Accessing files beyond MAX_PATH in C#/.NET

查看:282
本文介绍了在C#中访问文件超出MAX_PATH /。NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我需要使用.NET版本2.0在最高(使用一些现成写一个工具,不是一种选择此客户端的政治,商业和机密性/信任的原因)从一台服务器迁移到另一个文件在网络上。该服务器是本地团队文件服务器和某支球队的文件夹需要被迁移到其他服务器,以方便重组。其基本思想是,我们读到每个文件和流它在网络上下班时间和几天后的数据将被迁移。文件权限需要保留。由于这将需要数天时间(我们谈论到数GB数据,对于一些球队),我们需要每天晚上在文件迭代和比较修改日期和更新那些已改变。理论是,最终的新服务器将具有最多的文件的最新副本和用户可以切换到新的服务器。这当然不是这么简单,但我们有我们认为设计应该工作:)

I need to write a tool using .NET version 2.0 at highest (using something off the shelf is not an option for this client for political, commercial, and confidentiality/trust reasons) to migrate files from one server to another over the network. The servers are file servers for local teams, and certain team folders need to be migrated to other servers to facilitate a reorganisation. The basic idea is we read each file and stream it over the network out of hours and after a few days the data will be migrated. File permissions need to be preserved. As this will take a few days (we are talking up to several gigabytes of data, for some teams), we need to iterate over the files each night and compare the modification dates and update those that have changed. The theory is that eventually the new server will have an up to date copy of the files and the users can switch over to the new server. It is of course not quite this simple, but we have a design we think should work :)

问题

因此,在理论上,我们只需打开该文件,它流在网络上,并在另一端写的,对不对? :)

So in theory we just open the file, stream it over the network, and write it at the other end, right? :)

不幸的是,服务器上的自己,是在文件夹路径创建文件共享,如:

Unfortunately, on the servers themselves, the file shares were created at folder paths such as:

D:球队\Data\Team Shares\DIVISION\DEPARTMENT\NAME - 可以相当LONG\

D:\Data\Team Shares\DIVISION\DEPARTMENT\NAME OF TEAM - COULD BE FAIRLY LONG\

对于每个用户,此路径被映射到一个驱动器,例如,将作为\\SERVER\TEAMNAME共享并映射到T:驱动器

For each user, this path is mapped to a drive, for example it would be shared as \\SERVER\TEAMNAME and mapped to the T: drive.

这已经引起那里的文件,可见从T的情况:驱动器是 MAX_PATH 限制范围内,但是当服务器本身上本地查看,他们去的方式超越它。使用网络股为这个工具必须是通用的,对数百这些服务器上运行,我们不能访问这些文件,并有分辨哪个文件共享是那些我们应该移动和那些不是没有标准的方式 - 有没有命名约定的标准甚至。此外,偶尔有其他股的次新股,所以我们超过 MAX_PATH 限制了两次了!

This has caused the situation where the files as visible from the T: drive are within the MAX_PATH limitation, however when viewed locally on the server itself, they go way beyond it. We cannot access the files using the network shares as this tool needs to be generic, to run on hundreds of these servers, and there is no standard way to tell which file shares are ones we should be moving and those which are not - there is no naming convention standard even. Also, occasionally there are sub-shares of other shares and so we exceed the MAX_PATH limit twice over!

我我知道解决方法来指定使用\\?\前缀,它把路径为UNC路径,并允许为32K字的理论最大的路径。

I am aware of the workaround to specify paths using the "\\?\" prefix, which treats the path as a UNC path and allows a theoretical maximum of 32k characters.

这解决办法是在Win32 API的级别实现,System.IO命名空间是(大部分)基本上是一个薄各地本地Win32 API函数的包装,但是微软有有益交给调用关闭的API之前执行额外的(不正确)的验证。在这种情况下,.NET框架,是因为它声称拒绝路径?是一个无效路径字符。

This workaround is implemented at the Win32 API level, the System.IO namespace is (mostly) basically just a thin wrapper around native Win32 API functions, however Microsoft have "helpfully" implemented extra (incorrect) validation before handing the call off to the API. In this case, the .NET Framework is rejecting the path because it claims that '?' is an invalid path character.

所以我的问题是...是有我没有想过这样会让我解决这个无需完全重写几乎整个System.IO命名空间,使得P的负载/ Invoke的呼吁,只是移除这个恼人的验证?

So my question is... is there a way I haven't thought of that will allow me to work around this without having to completely rewrite the almost the whole System.IO namespace, making a load of P/Invoke calls, just to remove this annoying validation?

推荐答案

首创置业的团队做了究竟为什么这些选择都做了什么的变通是一个3部分组成的系列。如果你还没有读到,但我建议你这样做,因为它是信息的主题

The BCL team did a 3 part series on exactly why these choices were made and what the work arounds are. If you haven't read that yet I suggest you do as it's a great source of information on the subject

  • http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

这篇关于在C#中访问文件超出MAX_PATH /。NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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