Click here to Skip to main content
13,506,014 members
Rate this:
Please Sign up or sign in to vote.

I am trying to save a file whose path exceeds MAX_PATH (about 255) characters. I read that putting '\\?\' at the start of the path and using CreateFileW would work. However, this seems to work when the file path is short, which wouldn't be a problem without the extra '\\?\':
LPCWSTR path = "\\\\?\\C:\\test\testFile.txt";

However, when I changed the filename to a really long name so that the total path exceeded MAX_PATH, and I still had the \\?\ at the start, the file was not created!!! The handle was invalid and the last error code is 6 which is ERROR_INVALID_HANDLE.

So it seems that the work-around does not work. Does anyone know why?

Also, if I ever got this to work, is there a similar way to access the contents of a file whose path exceeds MAX_PATH?

I have thought about using symbolic links but would this CreateFileW method of it works as it seems less messy.

Many thanks for taking the time to read this :)
Posted 16-Jan-13 0:54am
Updated 16-Jan-13 1:37am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Maybe this article can lead you to a solution.
Hope this helps.
Jackie Lloyd 16-Jan-13 10:13am
Thank you - this is very useful but doesn't get round my problem. I appreciate your input.
phil.o 16-Jan-13 10:54am
Sorry, I'm not good at all with C++, but since I've read this article on yesterday I thought maybe you could get something useful out of it.
Jackie Lloyd 16-Jan-13 11:04am
Your suggestion was very usefu, but as I have since found there is no solution for what I want to do, it just isn't possible on NTFS. I will keep th elink to this for future :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There is a double drive specification in your path. Change it to:
LPCWSTR path = "\\\\?\\C:test\testFile.txt";

Then it should work. Once the file has been opened, you are accessing it by a handle and can read and write as usual.

But you should avoid using long file / path names if possible. They are not supported by the MFC, C-runtime functions (e.g. fopen(), chdir(), rename()), and Shell functions (those beginning with 'SH' like SHBrowseForFolder()).

When creating files, you must also check if the file system on the destination drive or share supports long file names.

Consider also that most applications don't support long names so that such files can't be opened by those.

By the comments it has been found the error occurs by using path components that exceed the component limit. The limit can be determined with the lpMaximumComponentLength parameter of the GetVolumeInformation() function.

See also the Naming Files, Paths, and Namespaces[^] MSDN article.
Jackie Lloyd 16-Jan-13 7:41am
Hello Jochen,
I am so sorry, I mistyped that line of code in the here, I couldn't copy and paste as its on a different machine. It was as you say, so that isn't the problem. My path really is path = L"\\\\?\\C:\\test\\testFile.txt" which works, it just doesn't work if I change testFile.txt to a really long name.

If the file is a .png, are you saying that I will not be able to do so if the file path is greater than 255?

Many thanks.
Jochen Arndt 16-Jan-13 7:56am
I just tested it here and it works (CreateFile() creates a file with a total path length of 280 characters). To get the error, call GetLastError() when CreateFile() returns INVALID_HANDLE_VALUE.

Possible error sources are:
The file system on the destination does not support long names (FAT is not supported, NTFS is).
The path does not exist yet. The path to the file must exist already.
Jackie Lloyd 16-Jan-13 9:33am
Thank you, I realised from your comment that mine would probably work if the entire path exceeds 255 but not (as I had been trying) if any one component in the (a folder or the file name) exceeds 255. This seems to be the case. So I guess there is no way to save files with a name that exceeds 255 or within a folder that exceeds 255.
Jochen Arndt 16-Jan-13 9:44am
You are right. There is also a limit for the components. See my updated answer with MSDN link.
Jackie Lloyd 16-Jan-13 10:15am
I have looked and couldn't see any way to exceed the 25 limit for each part of the path - is that what you think?
Jochen Arndt 16-Jan-13 10:25am
Yes. The limit is set by the file system. The common Windows file systems support a maximum of 255 characters per component. See also
Jackie Lloyd 16-Jan-13 10:33am
Thank you, that makes it very clear.
I have found this very interesting, even though I have found that there is no solution to my problem :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180417.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100