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.
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);
}
}
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
Set a directory as current and create directory in it.
Directory.SetCurrentDirectory(@"c:\sample");
Directory.CreateDirectory("test");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With