本文介绍了从目录及其子目录获取所有文件名及其路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码来获取文件夹中所有文件的名称.
I use this code to get all files name in a folder.
rootFolder="D:\\rootFolder\\";
var videofilenames = Directory
.GetFiles(rootFolder, "*.*", SearchOption.AllDirectories)
.Select(s => Path.GetFileName(s))
.Where(s =>s.EndsWith(".mp4") || s.EndsWith(".mkv") || s.EndsWith(".wav") || s.EndsWith(".avi"));
var videotop = videofilenames.Take(top);
foreach (var myscore in videotop)
{
string videofilename = myscore.ToString();
// and i store videofilename in an array
}
现在我也需要他们的路.但是我不知道如何改变它以达到我的目标.
now i need their path too.But I do not know how to change it to reach my aim.
thank you in advance.
推荐答案
DirectoryInfo dir=new DirectoryInfo(@"E:\Sidh\LabelHeight\LabelHeight\bin\Debug");
foreach (FileInfo file in dir.GetFiles("*.mkv"))
{
MessageBox.Show(file.FullName);
}
试试这个
try this
rootFolder="D:\\rootFolder\\";
static int top = 3810;
var videofilenames = Directory
.GetFiles(rootFolder, "*.*", SearchOption.AllDirectories)
.Where(s =>s.EndsWith(".mp4") || s.EndsWith(".mkv") || s.EndsWith(".wav") || s.EndsWith(".avi"));
var videotop = videofilenames.Take(top);
foreach (var myscore in videotop)
{
string Fullpath = myscore.ToString();
string videofilepath = Path.GetDirectoryName(Fullpath) + "\\";
string filename = Path.GetFileName(Fullpath);
string fileformat = Path.GetExtension(filename);
}
所以现在我拥有了一切:)
so now I have every thing :)
这篇关于从目录及其子目录获取所有文件名及其路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!