文件扩展名后的空格 ->奇怪的 FileInfo 行为
不知何故,一个文件出现在我的一个目录中,并且它的扩展名末尾有空格 - 它的名称是“test.txt”。奇怪的是 Directory.GetFiles()
返回我的路径 文件,但我无法使用 FileInfo
类检索文件信息。
错误在这里显现:
DirectoryInfo di = new DirectoryInfo("c:\\somedir");
FileInfo fi = di.GetFileSystemInfos("test*")[0] as FileInfo;
//correctly fi.FullName is "c:\somedir\test.txt "
//but fi.Exists==false (!)
FileInfo 类损坏了吗?我可以以某种方式检索有关该文件的信息吗?我真的不知道该文件是如何出现在我的文件系统上的,而且我无法重新创建更多文件。
我所有创建具有此类扩展名的新文件的尝试都失败了,但现在我的程序是 遇到它时崩溃。我可以轻松处理查找文件时的异常,但我真是这样 对此感到好奇!
Somehow a file has appeared in one of my directories, and it has space at the end of its extension -
its name is "test.txt ". The weird thing is that Directory.GetFiles()
returns me the path of this
file, but I'm unable to retrieve file information with FileInfo
class.
The error manifests here:
DirectoryInfo di = new DirectoryInfo("c:\\somedir");
FileInfo fi = di.GetFileSystemInfos("test*")[0] as FileInfo;
//correctly fi.FullName is "c:\somedir\test.txt "
//but fi.Exists==false (!)
Is FileInfo class broken? Can I somehow retrieve information about this file? I really don't know how did that file appear on my file system, and I am unable to recreate some more of them.
All of my attempts to create a new file with this type of extension have failed, but now my program is
crashing when encoutering it. I can easily handle the exception when finding the file, but boy am I
curious about this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以空格结尾文件名被记录为一个坏主意。
来自 MSDN "命名文件、路径和命名空间 (Windows)":
另外,知识库文章 "INFO:文件名结尾不支持空格或句点”:
DirectoryInfo
可能使用FindFirstFile()
等来生成目录列表。File.Exists
最有可能通过GetFileAttributes()
实现,它可能遇到与CreateFile()
相同的问题,并且会报告不存在的文件。因此,这不是 .NET 特有的问题,而是 Windows 本身的问题。
Ending file names with a space is documented as a Bad Idea.
From MSDN "Naming Files, Paths, and Namespaces (Windows)":
Also, the KB article "INFO: Filenames Ending with Space or Period Not Supported":
DirectoryInfo
probably usesFindFirstFile()
and friends to produce directory listings.File.Exists
is most likely implemented throughGetFileAttributes()
which probably suffers from the same problem asCreateFile()
and will report a nonexistent file.Hence, not a problem in .NET specifically, but in Windows itself.
是的,我知道这些文件。我也曾经遇到过这样的野兽。要摆脱它,我不知道 C# 中的编程方式,但好的旧命令行是你的朋友:
在给定文件夹中打开控制台窗口(或执行 cmd 并导航到该文件夹使用 cd 命令)。现在输入 dir /x 来检索此目录中文件的短名称。通过使用
del
或ren
命令使用此名称来删除或重命名文件。Yes i know of these files. I also got once such a beast thing. To get rid of it i don't know about a programming way in C#, but good old command line is your friend:
Open a console window in the given folder (or execute cmd and navigate to the folder with
cd
command). Now enterdir /x
to retrieve the shortname of the files in this directory. Use this name to delete or rename the file by using thedel
orren
command.如果使用
\\?\
路径语法:\\?\c:\somedir\test.txt
,则可以操作带有尾随空格(以及其他边缘情况)的文件。You can manipulate files with trailing spaces (among other edge cases) if you use the
\\?\
path syntax:\\?\c:\somedir\test.txt
.