Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi,

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";
Handle h = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
CloseHandle(h);


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
Updated 16-Jan-13 1:37am
v3

Hi,

Maybe this article can lead you to a solution.
Hope this helps.
 
Share this answer
 
Comments
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 :)
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.

[UPDATE]
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.
 
Share this answer
 
v2
Comments
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?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900