Directory.GetFiles查找不存在的文件 [英] Directory.GetFiles finds nonexisting files

查看:64
本文介绍了Directory.GetFiles查找不存在的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是偶然发现 System.IO.Directory GetFiles 方法的未记录行为.

I just stumbled upon an undocumented behavior of the GetFiles methods in System.IO.Directory.

只要传递给该方法的 searchPattern 参数包含保留的Windows设备名称,例如"nul.*" "aux.bmp" ,该方法返回一个包含不存在文件名的数组,例如 C:\ Users \ ft1 \ nul D:\ aux 等.

Whenever the searchPattern parameter passed to the method contains a reserved Windows device name, such as "nul.*" or "aux.bmp", the method returns an array containing the name of a nonexisting file, like C:\Users\ft1\nul or D:\aux, etc.

我想知道那些设备名称在上下文中是否具有特殊含义,例如".或"..",或者这仅仅是一种错误.无论如何,那似乎仍然很奇怪.例如,此代码段在C#中:

I wonder if those device names have a special meaning it that context, like "." or "..", or if this is just a sort of bug. Anyway, that still seems pretty weird. For example, this code snippet in C#:

string[] fileNames = Directory.GetFiles(@"C:\D:\..\..\...\", "con.txt");
foreach (string fileName in fileNames) Console.WriteLine(fileName);

打印

C:\D:\..\..\...\con

有任何线索吗?

推荐答案

这是已知的.这是关于命名的操作系统设计文件,路径和命名空间(Windows)

This is known. It is an operating system design regarding Naming Files, Paths, and Namespaces (Windows)

节选:

请勿将以下保留名称用作文件名:CON,PRN,AUX,NUL,COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,LPT1,LPT2,LPT3,LPT4,LPT5,LPT6,LPT7,LPT8和LPT9.也要避免在这些名称后面加上扩展名;例如,不建议使用NUL.txt.有关更多信息,请参见命名空间.

Do not use the following reserved names for the name of a file: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.

这些基本上是文件名别名(命名空间),因此它们始终全局存在(在每个文件夹中).如果您尝试枚举它们,则会将它们取回,因为它们确实存在.

These are basically filename aliases (namespaces), so they always exist globally (in every folder). If you attempt to enumerate them, you'll get them back because they do exist.

这篇关于Directory.GetFiles查找不存在的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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