string.Trim() 会从文件名中删除*有效*字符吗?
我正在创建一个类来存储文件名。 为此,我需要确切地知道哪些字符无效以及哪些字符作为前导/尾随字符无效。
Windows 资源管理器在命名文件时会自动修剪前导和尾随空白字符,因此我需要在构造文件名实例时修剪相同的字符。
我考虑过使用 string.Trim(),但假设它修剪的默认字符集与操作系统的无效前导/尾随文件名字符完全一致,那就太天真了。
string.Trim() 的 文档 表示它会修剪以下字符默认情况下: U+0009、U+000A、U+000B、U+000C、U+000D、U+0020、U+0085、U+00A0、U+1680、U+2000、U+2001、U+2002、U+ 2003、U+2004、U+2005、U+2006、U+2007、U+2008、U+2009、U+200A、U+200B、U+2028、U+2029、U+3000、U+FEFF
不幸的是,上述某些字符在文件中不是无效的,因为它们不在 System.IO.Path.GetInvalidFileNameChars 返回的字符集中。
我是否可以更正 string.Trim() 可能会从文件名中删除有效的前导/尾随字符,从而损坏文件名?
文件名的无效前导/尾随字符到底是什么Windows Vista 操作系统? 我知道它们不一定与文件系统本身相同,因为操作系统可以在不同的文件系统上运行。
I'm creating a class to store a filename. To do so, I need to know exactly which characters are invalid and exactly which characters are invalid as leading/trailing characters.
Windows Explorer trims leading and trailing white-space characters automatically when naming a file, so I need to trim the same characters when constructing a filename instance.
I thought about using string.Trim(), but it would be naive to assume the default set of characters it trims coincides exactly with the invalid leading/trailing filename characters of the OS.
Documentation for string.Trim() says that it trims the following characters by default:
U+0009, U+000A, U+000B, U+000C, U+000D, U+0020, U+0085, U+00A0, U+1680, U+2000, U+2001, U+2002, U+2003, U+2004, U+2005, U+2006, U+2007, U+2008, U+2009, U+200A, U+200B, U+2028, U+2029, U+3000, U+FEFF
Unfortunately, some of the above characters are NOT invalid in a file, because they aren't in the character set returned by System.IO.Path.GetInvalidFileNameChars.
Am I then correct that string.Trim() could potentially remove VALID leading/trailing characters from a filename, therefore corrupting the filename?
What exactly are the invalid leading/trailing characters for a filename in the Windows Vista OS? I understand that they are not necessarily the same as the file system itself, since the OS can run on different file systems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。 在类 UNIX 系统上更是如此,其中“X”是有效的文件名并且与“x”不同
Yes. Even more so on a UNIX-like system, where ' X' is a valid filename and distinct from ' x '
文件名可以以空格开头/结尾。 修剪会消除它们。
文件名不能包含
Filenames can start/end in spaces. Trim will eliminate them.
File names cannot contain
此代码运行并创建文件:
注意:使用上述代码创建的文件的实际名称似乎是“ file .foo”。 如果我在资源管理器中编辑文件名,则空间不存在,但是当我重新运行上面的代码时,它会替换该文件。
注意:我从 http://www.startvbdotnet.com/files/default.aspx 获取代码 并添加了空格
注意:我注意到 Vista 的资源管理器重命名不允许您在文件名之前或之后添加空格,因此您可以使用该方法创建“foo .txt”,但不能创建“foo.txt”。
This code runs and creates the file:
NOTE: the actual name of the file created with the above code appear to be " file . foo". If I edit the filename in Explorer the space isn't there but when I rerun the code above, it replaces the file.
NOTE: I took the code from http://www.startvbdotnet.com/files/default.aspx and added the spaces
NOTE: I notice that Vista's Explorer rename won't let you add the spaces before or after filename, so you can make "foo . txt" but not " foo.txt " using that method.