如何在 SaveFileDialog 中的默认文件名中设置长字符串(>260)?

发布于 2024-08-13 06:20:17 字数 245 浏览 10 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

自此以后,行同陌路 2024-08-20 06:20:17

简而言之,你不能; NTFS 的最大文件名大小为 256。

我的建议是缩短文件名。

In short, you can't; NTFS has a maximum filename size of 256.

My advice would be shorten your filename.

乖不如嘢 2024-08-20 06:20:17

尽管文件名可以超过 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

半暖夏伤 2024-08-20 06:20:17

您可以尝试使用文件名的短路径版本:

  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  public static extern int GetShortPathName(
     [MarshalAs(UnmanagedType.LPTStr)] System.String path,
     [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder shortPath,
     System.Int32 shortPathLength);

然后保存文件对话框将如下所示:

     string long_path = @"C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\1033\vsdebugui.dll";
     StringBuilder s = new StringBuilder(long_path.Length);
     int short_path_length = GetShortPathName(long_path, s, long_path.Length);
     string short_path = s.ToString();
     SaveFileDialog save_d = new SaveFileDialog();
     save_d.FileName = short_path;
     save_d.ShowDialog();

You could try the short path version of the file name by using:

  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  public static extern int GetShortPathName(
     [MarshalAs(UnmanagedType.LPTStr)] System.String path,
     [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder shortPath,
     System.Int32 shortPathLength);

Then the save file dialog would look something like this:

     string long_path = @"C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\1033\vsdebugui.dll";
     StringBuilder s = new StringBuilder(long_path.Length);
     int short_path_length = GetShortPathName(long_path, s, long_path.Length);
     string short_path = s.ToString();
     SaveFileDialog save_d = new SaveFileDialog();
     save_d.FileName = short_path;
     save_d.ShowDialog();
蓦然回首 2024-08-20 06:20:17

答案如下:

问:文件名的最大字符数是多少?

答:这取决于文件是在 FAT 还是 NTFS 分区上创建。 NTFS 分区上的最大文件名长度为 256 个字符,FAT 上的最大文件名长度为 11 个字符(8 个字符名称、.、3 个字符扩展名)。 NTFS 文件名保留大小写,而 FAT 文件名没有大小写概念(但是在 NTFS 上执行搜索等时会忽略大小写)。新的 VFAT 也有 256 个字符的文件名。

基本上

,操作系统决定最大文件大小,而不是您的应用程序。如果将来发布更好的支持更多字符的文件系统,您始终可以通过允许它来使您的软件向前兼容,并让用户知道当前路径何时太长。然而,以目前的技术进步,您不能使用大于256个字符的文件名。

The answer lies in the following:

Q: What is the maximum number of characters a filename can be?

A: This depends on if the file is being created on a FAT or NTFS partition. The maximum filename length on a NTFS partition is 256 characters, and 11 characters on FAT (8 character name, . , 3 character extension). NTFS filenames keep their case, whereas FAT filenames have no concept of case (however the case is ignored when performing a search etc on NTFS). There is the new VFAT which also has 256 character filenames.

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.

终止放荡 2024-08-20 06:20:17

\\?\ 添加到路径的开头。因此,D:\ 将变为 \\?\D:\。请参阅 http://msdn.microsoft.com 中的“最大路径长度限制” /en-us/library/aa365247.aspx

Add \\?\ to the beginning of the path. So D:\<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

撕心裂肺的伤痛 2024-08-20 06:20:17

你不能。

请记住,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?

瘫痪情歌 2024-08-20 06:20:17

正如其他人所提到的,没有解决此限制的好方法。但是问题不在于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文