我需要读取一个本地文本文件,该文件的每一行都有一个文件名。每个文件名都需要去掉它的扩展名。当我到达需要将修剪结果保存到另一个数组的部分时,我遇到了一些麻烦。
到目前为止我有:
string path = @"C:\Users\path\file.txt";
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
string result = Path.GetFileNameWithoutExtension(s);
//here I can print the result to the screen
//but I don't know how to save to another array for further manipulation
}
如果您需要任何进一步的说明,我会尽力让您更清楚。 提前致谢。
最佳答案
您也可以使用 Linq 执行此操作:
var path = @"C:\Users\path\file.txt";
var trimmed =
File.ReadAllLines(path)
.Select(Path.GetFileNameWithoutExtension)
.ToArray();
关于c# - 修剪文本文件中的每一行,然后将结果保存到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24962560/