本文介绍了C#FileInfo数组到字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储目录中文件的数组:

I have an array that stores files from a directory:

    string pathToDirectory = @"C:\files";
    System.IO.DirectoryInfo diDir = new DirectoryInfo(pathToDirectory);
    System.IO.FileInfo[] File_Array = diDir.GetFiles();

    foreach (FileInfo lfileInfo in File_Array)
    {
    }

我想尝试将此数组转换为字符串类型的数组,而不是FileInfo类型。请让我知道我该怎么做。任何帮助将不胜感激。

I want to try and convert this array into a string type array instead of a FileInfo type. Please let me know how I can go about doing this. Any help would be greatly appreciated.

推荐答案

使用静态类。它以字符串形式返回文件,而不是实例化您不需要的 FileInfo 实例

string[] files = Directory.GetFiles(pathToDirectory);

如果只希望文件名不带路径,请使用摆脱目录路径:

If you want just file names without path, then use Path to get rid of directory path:

var fileNames = Directory.GetFiles(pathToDirectory)
                         .Select(Path.GetFileName);

这篇关于C#FileInfo数组到字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:01
查看更多