PathTooLongException C # 4.5

I am having trouble copying some folder of 260+ characters (for example: F: \ NNNNNNNNNNNNNNN \ NNNNNNNNNNNN \ ROOT \ $ RECYCLE.BIN \ S-1-5-21-3299053755-4209892151-505108915-1000 \ $ RMSL3UNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNurnurny Sey Martyr Vysovskiy State University \ nNNNNNNNNNN \ nNNNNNNNNNN \ publish \ Application Files \ TNNNNNNNNNNNNN_N_N_N_N_N_N_N_NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN \ NNNNNNNNNN \ nNNNNNNNNNN \ NNNNNNNNNN \ nNNNNNNNNNNN \ nNNNNNNNNNN \ nNNNNNNNNNNN \ nNNNNNNNNNN \ nNNNNNNNNNNN \ nNNNNNNNNNN \ nNNNNNNNNNNN \ publish \ Application Files \ add \? \ or \? \ UNC \ (for example, \\? \ UNC \ "), just throw another ArgumentException. What am I doing wrong? What else can I do without using Directory.SetCurrentDirectory ()?

+4
source share
3 answers

Yes, using standard APIs will give you such restrictions (255 IIRC characters).

From .NET, you can use the AlphaFS project , which allows you to use very long paths (using the \\? \ Style) and mimics the System.IO namespace.

You can probably use this library as if you were using System.IO, for example: AlphaFS.Win32.Filesystem. File.Copy () instead of System.IO. File.Copy ()

If you do not want or cannot use AlphaFS, you will have to output the Win32 API

+3
source

Actually you need to call win32 with C #. We did it

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);
    }
}

Code example:

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");
}
+7
source

Microsoft TechNet , Delimon.Win32.I O Library (V4.0) System.IO

, :

System.IO.Directory.GetFiles

Delimon.Win32.IO.Directory.GetFiles

.

-:

Delimon.Win32.IO System.IO 32 767 .

.NET Framework 4.0 x86 x64. System.IO 260 240 (MAX_PATH 260 ). System.IO.PathTooLongException .NET.

+7

All Articles