如何在 SaveFileDialog 中的默认文件名中设置长字符串(>260)?
我正在使用 SaveFileDialog,并且必须在 FileName 中设置长字符串(longFileName)。字符串 longFileName 在运行时已知。
如果我设置了
saveFileDialog.FileName = longFileName ;
,我会得到System.IO.PathTooLongException
。
我该怎么做呢?
I am using a SaveFileDialog and have to set long string(longFileName) in FileName. String longFileName is known at Runtime.
If I set
saveFileDialog.FileName = longFileName ;
then I get System.IO.PathTooLongException
.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
简而言之,你不能; NTFS 的最大文件名大小为 256。
我的建议是缩短文件名。
In short, you can't; NTFS has a maximum filename size of 256.
My advice would be shorten your filename.
尽管文件名可以超过 260 个字符,但 API 可能会遇到一些奇怪的情况。此外,.net 并不直接支持它。
如果您想了解更多信息,请参阅 BCL 团队博客上关于该问题的三篇文章中的第一篇的链接:http://blogs.msdn.com/bclteam/archive/2007/02/13/long -paths-in-net-part-1-of-3-kim-hamilton.aspx
Although you can have file names longer than 260 characters, you can run into some weirdness with the API. Also, .net doesn't directly support it.
If you want more information, here is a link to the first of three posts on the BCL Team Blog about the issue: http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx
您可以尝试使用文件名的短路径版本:
然后保存文件对话框将如下所示:
You could try the short path version of the file name by using:
Then the save file dialog would look something like this:
答案如下:
基本上
,操作系统决定最大文件大小,而不是您的应用程序。如果将来发布更好的支持更多字符的文件系统,您始终可以通过允许它来使您的软件向前兼容,并让用户知道当前路径何时太长。然而,以目前的技术进步,您不能使用大于256个字符的文件名。
The answer lies in the following:
Source
Basically, the OS determines the max file size, not your app. If a better file system is released in the future that supports more characters you could always make your software forward compatible by allowing it, and letting the user know when the path is too long for now. However, with the current progress of technology, you cannot use a file name greater than 256 characters.
将
\\?\
添加到路径的开头。因此,D:\
将变为\\?\D:\
。请参阅 http://msdn.microsoft.com 中的“最大路径长度限制” /en-us/library/aa365247.aspxAdd
\\?\
to the beginning of the path. SoD:\<very_long_path>
would become\\?\D:\<very_long_path>
. See for reference "Maximum Path Length Limitation" in http://msdn.microsoft.com/en-us/library/aa365247.aspx你不能。
请记住,Windows 中的路径必须少于 256 个字符。 (除非你使用特殊技巧)
为什么你首先需要这么长的文件名?
You can't.
Remember that paths in Windows must be less than 256 characters. (Unless you use a special trick)
Why do you want such a long filename in the first place?
正如其他人所提到的,没有解决此限制的好方法。但是问题不在于 NTFS,而是在 Win32。使用 SFU,如果您确实愿意,您可以创建一些长得离谱的路径,但它使与本机 Win32 内容的互操作变得有趣。
您可能想尝试在路径前面添加 \\?\。不过我从未在.NET 上尝试过这个。
As others have mentioned, there is no good way around this limitation. However the problem is NOT with NTFS, its in Win32. Using SFU you can make some ridiculously long paths if you really want to, but it makes interop with native Win32 stuff interesting.
You might want to try prepending the path with \\?\. I've never tried this with .NET though.