Question

Recently I've run in to the issue with MAX_PATH in the File/Directory library in .Net, so I started looking into alternatives.

I'm attempting to rebuild files from raw data stored in a database and have had no issues doing so so far using new FileStream("G:\Location\temp.txt", FileMode.Create) etc, however I've been asked to change the structure of the rebuilt files, and thus run into the character limit.

I've attempted to use the example from Here, like this:

SafeFileHandle fileHandle = CreateFile(strDirectory,
EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero,
ECreationDisposition.OpenAlways, 0, IntPtr.Zero);

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

Where strDirectory = "G:\Really\Long\Folder\Structure\Temp.txt"

However, I'm getting an 'Access is denied' exception being thrown. I have full Read/Write permissions to the target folder, so I'm not sure what the problem could be.

Any help would be much appreciated.

Était-ce utile?

La solution

You must prefix the path with the string \\?\ in order to bypass the MAX_PATH limitation. This is documented in the CreateFile MSDN information where it states:

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming Files, Paths, and Namespaces.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top