如何处理非常长的文件名(使用TPath)? [英] How to handle very long file names (with TPath)?

查看:89
本文介绍了如何处理非常长的文件名(使用TPath)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以从Internet下载一些文件。文件名可能很长(100个字符)。
用户可以选择将这些文件保存在一个名称很长(200个字符)的文件夹中。
因此,路径的总长度超过了 260个字符

I have a program that downloads some files from Internet. The file name could be very long (100 chars). The user may choose to save these these files in a folder that has a very long name (200 chars). So, the total length of the path is over 260 chars.

我试图创建一个函数,该函数检查路径是否太长并截断文件名,因此整个路径将恰好为260个字符。但是,如果路径超过260个字符,则TPath类中的函数将无法正常工作。更确切地说,当路径超过260个字符时,GetDirectoryName会引发特定错误,因此我无法使用它从文件名中拆分文件夹。

I tried to create a function that checks if the path is too long and truncates the filename so the whole path will be exactly 260 chars. But functions in TPath class fails to work if the path is over 260 chars. More exactly GetDirectoryName raises a specific error when the path is over 260 chars, so I cannot use it to split the folder from file name.

Delphi中的主要设计缺陷?

我认为TPath在处理长文件名时引发错误是一个很大的缺陷。只需使用GetDirectoryName读取(而不是写入),就会使程序崩溃。 TPath应该允许处理长路径。也许仅当您尝试写入路径较长的文件时,它才应该引发错误。但即使那样也没有。 NTFS接受长路径。那么,为什么德尔福会强迫您坚持260?一些程序可以处理长路径。例如,我使用Total Commander(从不使用Explorer),所以我不受长文件名的影响。

A major design flaw in Delphi?
I think that TPath raising an error when working on long file names is a big flaw. Simply using GetDirectoryName to READ (not to write) will simply crash your program. TPath should allow processing long paths. MAYBE it should raise the error ONLY when you try to WRITE files with long path. But not even then. NTFS accepts long paths. So, why Delphi should force you to stick to 260? Some programs can handle longs paths. For example, I use Total Commander (never Explorer) so I am not affected by the long file name issue.

关于如何处理这种情况的任何想法吗?

注意:下载过程是自动进行的,因此我不会停止要求用户为每个文件输入少于260个字符的新文件名。我想安静地处理这个问题。

Any idea on how to treat this case?
Note: The download process is automated so I won't stop to ask the user to enter a new file name for each file that fails to be under 260 chars. I want to handle this silently.

推荐答案

我个人认为, TPath 在这里根本是错误的。断言Windows路径不能大于260个字符只是在否认现实。而且,拒绝您对路径执行文本处理的功能确实是莫名其妙的。在我看来,应该避免使用 TPath

Personally, my opinion is that TPath is simply wrong here. To assert that Windows paths cannot be greater than 260 characters is simply denying reality. What's more, to deny you the ability to perform text processing on paths is really quite inexplicable. In my opinion then, TPath should be avoided.

这会带您回到美好的过去。您可以从 SysUtils 调用 ExtractFileDir 。它的工作原理和以往一样好。

Which leads you back to the good old days. You can call ExtractFileDir from SysUtils. It works as well as it ever did.

这篇关于如何处理非常长的文件名(使用TPath)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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