是否可以将文本文件数据存储在只知道其扩展名的变量中?我在考虑像
这样的东西string path = @"D:\MyTest";
var txt = File.ReadAllLines(path + "*.txt");
答案 0 :(得分:3)
如果您要查找扩展名为.txt
的文件,并且已找到其中任何一个,请加载内容:
string path = @"D:\MyTest";
// Please, notice that you'll have an array string[] txt
// If you want just String txt output (not array) change ReadAllLines to ReadAllText
var txt = Directory
.EnumerateFiles(path, "*.txt")
// If you can have many text files you may want to add a filter here:
// .Where(file => ...)
.Select(file => File.ReadAllLines(file))
.FirstOrDefault();
答案 1 :(得分:1)
是的,如果文件名中没有扩展名。无需指定扩展名。
string path = @"D:\MyTest";
var txt = File.ReadAllLines(path);