How can I use FileInfo class, avoiding PathTooLong

2020-01-29 08:32发布

How can I use (to avoid PathTooLongException):

System.IO.FileInfo

with paths bigger than 260 chars?

Are there similar classes/methods that return the same result of FileInfo class?

标签: c# exception
4条回答
再贱就再见
2楼-- · 2020-01-29 09:20

From what I know it is not easily possible. While it is possible to use workaround for streams as phoenix mentioned, it is not possible for file names handling. Internally every class that works with file names perform checks for long file names.

You can instantiate FileInfo and fill private memebers using reflection (however this is not recommended) and get FileInfo pointing to file with long path. But when you try to use this object you will still receive PathTooLongException exceptions, because for example, Path class (used heavily by FileInfo) checks for long path on every method call.

So, there is only one right way to get problem free long path support - implement your own set of classes that will mimic FileInfo behavior. It is not very complex (only security maybe), but time-consuming.

Update: Here even two ready solutions for this problem: AlpfaFS and Zeta Long Paths

查看更多
男人必须洒脱
3楼-- · 2020-01-29 09:31

There's a great library on Microsoft TechNet for overcoming the long filenames problem, it's called Delimon.Win32.I​O Library (V4.0) and it has its own versions of key methods from System.IO

For example, you would replace:

System.IO.Directory.GetFiles 

with

Delimon.Win32.IO.Directory.GetFiles

which will let you handle long files and folders.

From the website:

Delimon.Win32.IO replaces basic file functions of System.IO and supports File & Folder names up to up to 32,767 Characters.

This Library is written on .NET Framework 4.0 and can be used either on x86 & x64 systems. The File & Folder limitations of the standard System.IO namespace can work with files that have 260 characters in a filename and 240 characters in a folder name (MAX_PATH is usually configured as 260 characters). Typically you run into the System.IO.PathTooLongException Error with the Standard .NET Library.

查看更多
时光不老,我们不散
4楼-- · 2020-01-29 09:33

Here at work we deal with long paths quite frequently, and we therefore had to basically roll our own System.IO to do it. Well not really, but we rewrote File, Directory, FileInfo, DirectoryInfo and Path just to name a few. The basic premise is that it's all possible from a Win32 API perspective, so all you really need to do at the end of the day is invoke the Unicode versions of the Win32 API functions, and then you're good. It's alot of work, and can be a pain in the ass at times, but there's really no better way to do it.

查看更多
男人必须洒脱
5楼-- · 2020-01-29 09:34

I only needed to use the FullName property but was also receiving the PathTooLongException.

Using reflection to extract the FullPath value was enough to solve my problem:

private static string GetFullPath(FileInfo src)
{
    return (string)src.GetType()
        .GetField("FullPath", BindingFlags.Instance|BindingFlags.NonPublic)
        .GetValue(src);
}
查看更多
登录 后发表回答