Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Directory.CreateDirectory(path) supporting long paths

Tags:

c#

.net

Just wondering if there is an alternative to Directory.CreateDirectory() as I am trying to create a directory which is longer then 260 characters, although the filename isn't long but the directory path is.

OR

If there is any trick using which I can point CreateDirectory to create a folder at this location without giving full path of directory. As I am creating folders within folders and so on.. There must be some legitimated way to do it.

There was problem with string which I now saving in hidden label so it's not a problem anymore.

like image 363
Muhammad Raja Avatar asked Jan 06 '12 09:01

Muhammad Raja


3 Answers

The simple solution is would be to use a unc enabled path which will would allow file paths of up to approx 32767 chars

string longPathEnabledFileName = Path.ToLongPath("C:\SomeVeryLongPath\...."); 
FileStream fs = new FileStream(longPathEnabledFileName);

This would simply prepend the path with \\?\ which tells the framework to bypass the MAX_PATH limitation of 260 chars. Unfortunately, The prefix of \\?\ is not supported within .Net at the time of writing (as of version 4.0)

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

This leaves us with a WinApi solution and reference Kernel32.dll to use the SafeFileHandle. Kim Hamilton from the BCL team has blogged a series of workarounds to the MAX_PATH limitations here of (Part 2 shows how to use the winapi functions) with a code snippet included here for reference:

// This code snippet is provided under the Microsoft Permissive License.
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern SafeFileHandle CreateFile(
    string lpFileName,
    EFileAccess dwDesiredAccess,
    EFileShare dwShareMode,
    IntPtr lpSecurityAttributes,
    ECreationDisposition dwCreationDisposition,
    EFileAttributes dwFlagsAndAttributes,
    IntPtr hTemplateFile);

public static void TestCreateAndWrite(string fileName) {

    string formattedName = @"\\?\" + fileName;
    // Create a file with generic write access
    SafeFileHandle fileHandle = CreateFile(formattedName,
        EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero,
        ECreationDisposition.CreateAlways, 0, IntPtr.Zero);

    // Check for errors
    int lastWin32Error = Marshal.GetLastWin32Error();
    if (fileHandle.IsInvalid) {
        throw new System.ComponentModel.Win32Exception(lastWin32Error);
    }

    // Pass the file handle to FileStream. FileStream will close the
    // handle
    using (FileStream fs = new FileStream(fileHandle,
                                    FileAccess.Write)) {
        fs.WriteByte(80);
        fs.WriteByte(81);
        fs.WriteByte(83);
        fs.WriteByte(84);
    }
}
  • http://blogs.msdn.com/b/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

  • http://blogs.msdn.com/b/bclteam/archive/2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton.aspx

  • http://blogs.msdn.com/b/bclteam/archive/2008/07/07/long-paths-in-net-part-3-of-3-redux-kim-hamilton.aspx

There is also a library that encapsulates all this work over at google code called zeta long paths

  • http://code.google.com/p/zetalongpaths/
like image 77
Anastasiosyal Avatar answered Nov 15 '22 05:11

Anastasiosyal


Set a directory as current and create directory in it.

 Directory.SetCurrentDirectory(@"c:\sample");
 Directory.CreateDirectory("test");
like image 30
KV Prajapati Avatar answered Nov 15 '22 03:11

KV Prajapati


The alternative way is use DirectoryInfo class and method DirectoryInfo.Create.

I didn't try that, but MSDN shows that it isn't throw exception when you use too long path.

EDIT:

Also, I've find something that can help you to solve your problem. Take a look at this code

like image 2
Chuck Norris Avatar answered Nov 15 '22 05:11

Chuck Norris