尝试这个:
// Set up List View
listViewFiles.View = View.Details;
listViewFiles.Columns.Clear();
listViewFiles.Columns.Add("File name");
listViewFiles.Columns.Add("File path");
// Populate with files and file paths
foreach (string filePath in Directory.GetFiles(path, fileType, SearchOption.AllDirectories))
{
string fileName = Path.GetFileName(filePath);
listView1.Items.Add(fileName).SubItems.Add(new FileInfo(fileName).DirectoryName);
}
编辑:
就个人而言,我发现为这种事情实例化 DirectoryInfo 更容易,它为您填充了许多有用的字段。所以你可以这样做:
DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo fi in di.GetFiles(fileType, SearchOption.AllDirectories))
listViewFiles.Items.Add(fi.Name).SubItems.Add(fi.DirectoryName);