6

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();
    }
sthiers
  • 3,489
  • 5
  • 34
  • 47
  • In C# try this library: http://zetalongpaths.codeplex.com/....or if it's in WIN32 then you need to use the special prefix to allow for longer file names. see: http://msdn.microsoft.com/en-us/library/aa365247.aspx – Colin Smith Jul 19 '13 at 13:43
  • 1
    There's a comment in some of the API methods that you can use "\\?\" with the Unicode calls as a prefix for long paths, but I don't know if that translates into command-line tools – Rup Jul 19 '13 at 13:43

3 Answers3

14

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.

Colin Smith
  • 12,375
  • 4
  • 39
  • 47
2

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    You wouldn't rename an existing directory to a short name, then create a long sub-directory and then rename it back. No, not a good solution. Also, you can't do that if files of `A` are in use. – Thomas Weller Apr 22 '15 at 20:45
1

How about something like this:

var path = "C:\\";
while (path.Length <= 260)
{
    path = Path.Combine(path, "Another Sub Directory");
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232