保存和访问文件时,尝试超过MAX_PATH [英] Trying to exceed MAX_PATH when saving and accessing files

查看:255
本文介绍了保存和访问文件时,尝试超过MAX_PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试保存路径超过MAX_PATH(约255)个字符的文件。我读到在路径的开头放置''\\?\''并使用CreateFileW可以工作。但是,当文件路径很短时,这似乎有效,没有额外的''\\?\''就不会有问题:

 LPCWSTR path =\\\\?\\C:\\test\testFile.txt; 
句柄h = CreateFileW(path,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
CloseHandle(h);





然而,当我将文件名更改为一个非常长的名称,以便超出总路径MAX_PATH,我在开始时仍然有\\?\,文件没有创建!句柄无效,最后一个错误代码是6,即ERROR_INVALID_HANDLE。



所以似乎解决方法不起作用。有谁知道为什么?



另外,如果我让它工作,有没有类似的方法来访问路径超过MAX_PATH的文件的内容?



我已经考虑过使用符号链接,但是这个CreateFileW方法的工作原理似乎不那么混乱了。



非常感谢花时间阅读本文:)

解决方案





也许这篇文章可以引导您找到解决方案。

希望这会有所帮助。

路径中有双驱动器规范。将其更改为:

 LPCWSTR path =\\\\?\\C:test\testFile.txt; 





然后它应该工作。打开文件后,您可以通过句柄访问它,并且可以照常读写。



但是如果可能的话,你应该避免使用长文件/路径名。它们不受MFC,C运行时函数支持(例如 fopen() chdir() rename())和Shell函数(以''SH'开头的那些函数,如 SHBrowseForFolder())。



创建文件时,还必须检查目标驱动器或共享上的文件系统是否支持长文件名。



还要考虑大多数应用程序都不支持长名称,因此这些文件无法打开。



[更新]

通过注释发现,使用超出组件限制的路径组件会发生错误。可以使用 GetVolumeInformation()函数的 lpMaximumComponentLength 参数确定限制。



另请参阅命名文件,路径和命名空间 [ ^ ] MSDN文章。


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 :)

解决方案

Hi,

Maybe this article can lead you to a solution.
Hope this helps.


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.


这篇关于保存和访问文件时,尝试超过MAX_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆