gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:15:40 25 4
gpt4 key购买 nike

        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));

关于c# - 查找以用户给定的变量开头的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34529408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com