加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 文件扩展名后的空格 – >奇怪的FileInfo行为

发布时间:2020-12-15 04:30:52 所属栏目:百科 来源:网络整理
导读:不知何故,一个文件出现在我的一个目录中,并且在其扩展名末尾有空格 – 它的名字是“test.txt”.奇怪的是,Directory.GetFiles()返回了我的路径 文件,但我无法使用FileInfo类检索文件信息. 错误在此处显示: DirectoryInfo di = new DirectoryInfo("c:somedi
不知何故,一个文件出现在我的一个目录中,并且在其扩展名末尾有空格 –
它的名字是“test.txt”.奇怪的是,Directory.GetFiles()返回了我的路径
文件,但我无法使用FileInfo类检索文件信息.

错误在此处显示:

DirectoryInfo di = new DirectoryInfo("c:somedir");
FileInfo fi = di.GetFileSystemInfos("test*")[0] as FileInfo; 
//correctly fi.FullName is "c:somedirtest.txt "
//but fi.Exists==false (!)

FileInfo类是否已损坏?我可以以某种方式检索有关此文件的信息吗?我真的不知道该文件是如何出现在我的文件系统上的,我无法再重新创建它们.

我尝试使用这种类型的扩展创建新文件的所有尝试都失败了,但现在我的程序是
在装上它时会崩溃.我可以在找到文件时轻松处理异常,但我是男孩
好奇这个!

解决方法

使用空格结束文件名被记录为坏主意.

来自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.

此外,知识库文章“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可能使用FindFirstFile()和朋友来生成目录列表. File.Exists很可能是通过GetFileAttributes()实现的,它可能遇到与CreateFile()相同的问题并报告一个不存在的文件.

因此,在.NET中没有特别的问题,但在Windows本身.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读