Skip to main content
 首页 » 编程设计

c#之查找以用户给定的变量开头的文件

2024年02月05日9虾米哥

        System.IO.FileInfo[] fileNames = dir.GetFiles("*check*.*"); 

这会返回任何带有名称检查的文件。但我想要的是返回那些以用户给定的字符串开头的文件。 示例

string word = Console.Readkey(); 
System.IO.FileInfo[] fileNames = dir.GetFiles("*word*.*"); 

我试过了

System.IO.FileInfo[] fileNames = dir.GetFiles("*"+word+"*".*"); 

但这没有用

请您参考如下方法:

首先,Console.ReadKey() 读取一个 key ,我认为您不希望它这样做,因此将其更改为 Console.ReadLine() .

话虽如此,如果您想使用变量 word,则不要将其放入字符串中。将它放在外面并与模式的其余部分连接或使用 String.Format()。

所以使用这段代码:

string word = Console.ReadLine(); 
System.IO.FileInfo[] fileNames = dir.GetFiles(String.Format("{0}*.*", word));