6

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.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
Muhammad Raja
  • 1,970
  • 3
  • 28
  • 46

4 Answers4

2

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

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • thanks for your reply, i tried it but still getting same exception. – Muhammad Raja Jan 06 '12 at 09:50
  • @ mesiesta, thanks for your edit, i actually looked at that earlier, its exactly like something i need but its for files however i need something similar for folder, i wonder even if i try to change the code for folders and it will gonna work – Muhammad Raja Jan 06 '12 at 11:03
2

Set a directory as current and create directory in it.

 Directory.SetCurrentDirectory(@"c:\sample");
 Directory.CreateDirectory("test");
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 2
    thanks for your reply, I tried it but still getting same exception as its setting the current directory but when you create a new directory it do it as "CurrentDirectory" + "CreateDirectory" – Muhammad Raja Jan 06 '12 at 09:55
2

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)

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

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

Anastasiosyal
  • 6,494
  • 6
  • 34
  • 40
  • I looked into it, but i wonder how can i change Windows API, or i just missed it ? – Muhammad Raja Jan 06 '12 at 10:56
  • Hmmm, yup, there is no such thing as Path.ToLongPath as it is just a suggestion for the future from BCL. The .net framework unfortunately does not support the simple solution of \\?\ which means that you would need to resort to using the WinApi PInvoke functions that are described in http://blogs.msdn.com/b/bclteam/archive/2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton.aspx or use zeta long paths - Editing post to reflect this – Anastasiosyal Jan 06 '12 at 11:20
  • 1
    thanks for your comment , I was actually looking at zeta but using that dll i can't create a directory, its not mentioned there neither i can find it, however they say you can get files count which am afraid i dont need in this context, Cheers – Muhammad Raja Jan 06 '12 at 11:27
  • Thanks Anastasiosyal! :) @TimeToThine, ZetaLongPaths.ZlpIOHelper.CreateDirectory(longPath); – Rami A. May 01 '12 at 03:26
1

What about splitting your prospective path using the '\' character, then looping through each item, seeing if a directory exists, if it doesn't - create it, then use

Directory.SetCurrentDirectory(directoryName);
Antony Koch
  • 2,043
  • 1
  • 16
  • 23