您可以使用以下代码块之一:
DirectoryInfo dir = new DirectoryInfo(@"C:\");
var files = (from f in dir.GetFiles() select f.FullName.Replace(" ", "_"));
或者:
DirectoryInfo dir = new DirectoryInfo(@"C:\");
var files = (from f in dir.GetFiles() select f.FullName);
var fileNames = (from fn in files select fn.Replace(" ", "_"));
或者:
DirectoryInfo dir = new DirectoryInfo(@"C:\");
var files = (from f in dir.GetFiles() select f.FullName);
var fileNames = files.Select(s => s.Replace(" ", "_"));
或(坏主意):
DirectoryInfo dir = new DirectoryInfo(@"C:\");
var files = (from f in dir.GetFiles() select f.FullName);
var fileNames = files.ToList().ConvertAll(s => s.Replace(" ", "_"));
编辑 1:但我建议使用 select 函数,最好不要为 IQueryable 等延迟对象调用 .ToList() 方法。