Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create directory exceeding MAX_PATH

For testing purpose, I would like to create on disk a directory which exceeds the Windows MAX_PATH limit. How can I do that?

(I tried Powershell, cmd, windows explorer => it's blocked.)

Edited: The use of ZlpIOHelper from ZetaLongPaths library allows to do that whereas the standard Directory class throws the dreaded exception:

        static void Main(string[] args)
    {
        var path = @"d:\temp\";
        var dirName = "LooooooooooooooooooooooooooooooooooooooooooooongSubDirectory";
        while (path.Length <= 280)
        {
            path = Path.Combine(path, dirName);
            ZlpIOHelper.CreateDirectory(path); //Directory.CreateDirectory(path);
        }
        Console.WriteLine(path);
        Console.ReadLine();
    }
like image 565
sthiers Avatar asked Jul 19 '13 13:07

sthiers


People also ask

How do I get Windows 10 to accept file paths over 260 characters?

To enable long file paths in Windows, open Registry Editor, create a new DWORD named "LongPathsEnabled" in "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem" and set the value to 1.

Why does Windows have a 260 character limit?

The 260-character path limit is due to fixed buffers in the runtime library for DOS/Windows path processing (e.g. ANSI <=> Unicode, working directory, and DOS<=>NT path conversion).


2 Answers

In WIN32 you need to use the special "\\?\" prefix to allow for longer file names.

See: http://msdn.microsoft.com/en-us/library/aa365247.aspx

For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system. For example, if the file system supports large paths and file names, you can exceed the MAX_PATH limits that are otherwise enforced by the Windows APIs. For more information about the normal maximum path limitation, see the previous section Maximum Path Length Limitation.

As you are using C# then try this library which will save you having to do all the PInvokes to the WIN32 file API and adding the prefix to the paths.

  • http://zetalongpaths.codeplex.com/
like image 138
CSmith Avatar answered Sep 19 '22 06:09

CSmith


The easiest solution is to create a directory c:\A\averylongnamethatgoesonandonandon..., and then rename C:\A to something much longer. Windows does not check every child of A to see if the name of that child would exceed MAX_PATH.

like image 28
MSalters Avatar answered Sep 17 '22 06:09

MSalters