转换一个FileInfo数组转换成String数组C# [英] convert a FileInfo array into a String array C#

查看:2366
本文介绍了转换一个FileInfo数组转换成String数组C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个FileInfo数组像这样

I create a FileInfo array like this

 try
            {
                DirectoryInfo Dir = new DirectoryInfo(DirPath);
                FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);


                foreach (FileInfo FI in FileList)
                {
                    Console.WriteLine(FI.FullName);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

和这个数组保存在文件夹中的所有文件名= DirPath

And this array holds all the file names in folder = DirPath

我想通过FileInfo的阵列循环的,并将其复制到字符串数组中。 ?这是好的或者是有一个更清洁的方法

I thought of looping through the FileInfo array and copy it to a String array. Is this ok or is there a much cleaner method ?

推荐答案

使用LINQ:

FileList.Select(f => f.FullName).ToArray();



另外,使用目录,你可以得到的文件名直接。

Alternatively, using Directory you can get filenames directly.

string[] fileList = Directory.GetFiles(DirPath, "*.*", 
                                       SearchOption.AllDirectories);

这篇关于转换一个FileInfo数组转换成String数组C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆