convert a FileInfo array into a String array C#

前端 未结 4 1153
春和景丽
春和景丽 2021-01-05 05:17

I create a FileInfo array like this

 try
            {
                DirectoryInfo Dir = new DirectoryInfo(DirPath);
                FileInfo[] FileList =          


        
相关标签:
4条回答
  • 2021-01-05 05:53

    the linq is a great soluction, but for the persons who don't want to use linq, i made this function:

        static string BlastWriteFile(FileInfo file)
        {
            string blasfile = " ";
            using (StreamReader sr = file.OpenText())
            {
                string s = " ";
                while ((s = sr.ReadLine()) != null)
                {
                    blasfile = blasfile + s + "\n";
                    Console.WriteLine();
                }
            }
            return blasfile;
        }
    
    0 讨论(0)
  • 2021-01-05 06:00

    Try this one

    DirectoryInfo directory = new DirectoryInfo("your path");
    List<string> Files = (directory.GetFiles().Where(file => file.LastWriteTime >= date_value)).Select(f => f.Name).ToList();
    

    If you don't want a filter with date, you can simply convert with the below code

    List<string> logFiles = directory.GetFiles().Select(f => f.Name).ToList();
    

    If you need the full path of the file, you can use FullName instead of Name.

    0 讨论(0)
  • 2021-01-05 06:07

    Using LINQ:

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

    Alternatively, using Directory you can get filenames directly.

    string[] fileList = Directory.GetFiles(DirPath, "*.*", 
                                           SearchOption.AllDirectories);
    
    0 讨论(0)
  • 2021-01-05 06:11

    If you want to go the other way (convert string array into FileInfo's) you can use the following:

    string[] files;
    var fileInfos = files.Select(f => new FileInfo(f));
    List<FileInfo> infos = fileInfos.ToList<FileInfo>();
    
    0 讨论(0)
提交回复
热议问题