PathTooLongException C#4.5

Его*_*тов 4 c# io exception visual-studio-2012 pathtoolongexception

我有一些复制文件夹260+字符(例如麻烦:F:\ NNNNNNNNNNNNNNNN\NNNNNNNNNNN\ROOT\$ RECYCLE.BIN\S-1-5-21-3299053755-4209892151-505108915-1000\$ RMSL3U8 \nnnnnnnnn为NNNNNNNN\NNNNNNNNNNN\NNNNNNNNNN\NNNNNNNNNN \发布\应用程序文件\ TNNNNNNNNNNNN_1_0_0_0\NNNNNNNNNNNN.exe.manifest)与非标准DrectoryInfo.Create其他一些地方(); 添加\?\或\?\ UNC \(如 "\\?\ UNC \")只是把另一ArgumentException的.我究竟做错了什么?如果不使用Directory.SetCurrentDirectory(),我还能做些什么?

Vin*_*oth 7

实际上你需要从c#调用win32.我们做到了这一点

using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public static class LongPath
{
    static class Win32Native
    {
        [StructLayout(LayoutKind.Sequential)]
        public class SECURITY_ATTRIBUTES
        {
            public int nLength;
            public IntPtr pSecurityDescriptor;
            public int bInheritHandle;
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool CreateDirectory(string lpPathName, SECURITY_ATTRIBUTES lpSecurityAttributes);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
    }

    public static bool CreateDirectory(string path)
    {
        return Win32Native.CreateDirectory(String.Concat(@"\\?\", path), null);
    }

    public static FileStream Open(string path, FileMode mode, FileAccess access)
    {
        SafeFileHandle handle = Win32Native.CreateFile(String.Concat(@"\\?\", path), (int)0x10000000, FileShare.None, null, mode, (int)0x00000080, IntPtr.Zero);
        if (handle.IsInvalid)
        {
            throw new System.ComponentModel.Win32Exception();
        }
        return new FileStream(handle, access);
    }
}
Run Code Online (Sandbox Code Playgroud)

示例代码:

string path = @"c:\".PadRight(255, 'a');
LongPath.CreateDirectory(path);

path = String.Concat(path, @"\", "".PadRight(255, 'a'));
LongPath.CreateDirectory(path);

string filename = Path.Combine(path, "test.txt");

FileStream fs = LongPath.Open(filename, FileMode.CreateNew, FileAccess.Write);

using (StreamWriter sw = new StreamWriter(fs))
{
    sw.WriteLine("abc");
}
Run Code Online (Sandbox Code Playgroud)


Tri*_*gen 7

Microsoft TechNet上有一个很棒的库,用于克服长文件名问题,它叫做Delimon.Win32.I O Library(V4.0),它有自己的System.IO关键方法版本

例如,您将替换:

System.IO.Directory.GetFiles
Run Code Online (Sandbox Code Playgroud)

Delimon.Win32.IO.Directory.GetFiles
Run Code Online (Sandbox Code Playgroud)

这将让你处理长文件和文件夹.

来自网站:

Delimon.Win32.IO取代了System.IO的基本文件功能,并支持最多32,767个字符的文件和文件夹名称.

此库是在.NET Framework 4.0上编写的,可以在x86和x64系统上使用.标准System.IO命名空间的文件和文件夹限制可以使用文件名中包含260个字符且文件夹名称中包含240个字符的文件(MAX_PATH通常配置为260个字符).通常,您使用标准.NET库遇到System.IO.PathTooLongException错误.


mba*_*emy 5

是的,使用标准 API 会给您带来这种限制(255 个字符 IIRC)。

在 .NET 中,您可以使用AlphaFS 项目,该项目允许您使用很长的路径(使用“\\?\”样式)并模仿 System.IO 命名空间。

您可能能够像使用 System.IO 一样使用此库,例如:AlphaFS.Win32.Filesystem。File.Copy()而不是 System.IO。文件.复制()

如果您不想或无法使用 AlphaFS,则必须调用 Win32 API