如何将List <system.io.fileinfo>的类型转换为List <string>?</string> </system.io.fileinfo>

时间:2011-04-14 21:57:36

标签: c# regex directory

我需要将这种类型的List转换为字符串列表:

var fileName = (new DirectoryInfo(filePath)).GetFiles(".", SearchOption.AllDirectories).Where(a => Regex.IsMatch(a.Name, "^[^*]*" + logSelected + ".jpg$")).ToList();
有人知道怎么回事?

4 个答案:

答案 0 :(得分:4)

var fileName = (new DirectoryInfo(filePath))
.GetFiles(".", SearchOption.AllDirectories)
.Where(a => Regex.IsMatch(a.Name, "^[^*]*" + logSelected + ".jpg$"))
.Select(a => a.Name)
.ToList();

或者在Select方法中更改所需属性的a.Name

答案 1 :(得分:0)

尝试添加.Select(fi => fi.Name)

List<string> fileNames = (new DirectoryInfo(filePath)).GetFiles(".", SearchOption.AllDirectories)
            .Where(a => Regex.IsMatch(a.Name, "^[^*]*" + logSelected + ".jpg$"))
            .Select(fi => fi.Name)
            .ToList();

答案 2 :(得分:0)

var fileName = new DirectoryInfo(filePath)
                   .GetFiles(".", SearchOption.AllDirectories)
                   .Where(a => Regex.IsMatch(a.Name, "^[^*]*" + logSelected + ".jpg$"))
                   .Select(x => x.Name)
                   .ToList();

答案 3 :(得分:0)

            var fileName = (new DirectoryInfo(filePath)).GetFiles(".", SearchOption.AllDirectories)
            .Where(a => Regex.IsMatch(a.Name, "^[^]" + logSelected + ".jpg$"))
            .Select(fi=>fi.FullName)
            .ToList();