文件扩展名后的空格 ->奇怪的 FileInfo 行为

发布于 2024-08-31 01:01:38 字数 545 浏览 2 评论 0原文

不知何故,一个文件出现在我的一个目录中,并且它的扩展名末尾有空格 - 它的名称是“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 技术交流群。

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

发布评论

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

评论(3

甜妞爱困 2024-09-07 01:01:38

以空格结尾文件名被记录为一个坏主意。

来自 MSDN "命名文件、路径和命名空间 (Windows)"

  • 不要以空格或句点结尾文件或目录名。尽管底层文件系统可能支持此类名称,但 Windows shell 和用户界面不支持。

另外,知识库文章 "INFO:文件名结尾不支持空格或句点”

当 Macintosh 客户端在 Windows NT 服务器上创建文件时可能会出现问题。不执行删除尾随空格和句点的代码,Macintosh 用户将获得正确标点的文件名。 Win32 API FindFirstFile()FindNextFile() 返回以空格或句点结尾的文件名;但是,无法使用 Win32 API 创建或打开该文件。

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)":

  • Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not.

Also, the KB article "INFO: Filenames Ending with Space or Period Not Supported":

Problems can arise when a Macintosh client creates a file on a Windows NT server. The code to remove trailing spaces and periods is not carried out and the Macintosh user gets the correctly punctuated filename. The Win32 APIs FindFirstFile() and FindNextFile() return a filename that ends in a space or in a period; however, there is no way to create or open the file using the Win32 API.

DirectoryInfo probably uses FindFirstFile() and friends to produce directory listings. File.Exists is most likely implemented through GetFileAttributes() which probably suffers from the same problem as CreateFile() and will report a nonexistent file.

Hence, not a problem in .NET specifically, but in Windows itself.

猫九 2024-09-07 01:01:38

是的,我知道这些文件。我也曾经遇到过这样的野兽。要摆脱它,我不知道 C# 中的编程方式,但好的旧命令行是你的朋友:

在给定文件夹中打开控制台窗口(或执行 cmd 并导航到该文件夹使用 cd 命令)。现在输入 dir /x 来检索此目录中文件的短名称。通过使用 delren 命令使用此名称来删除或重命名文件。

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 enter dir /x to retrieve the shortname of the files in this directory. Use this name to delete or rename the file by using the del or ren command.

神回复 2024-09-07 01:01:38

如果使用 \\?\ 路径语法:\\?\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.

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