定义方法
public void GetFileName(string DirName,string FileName)
{
DirectoryInfo folders = new DirectoryInfo(DirName);
//如果非根路径且是系统文件夹则跳过
if (null != dir.Parent && dir.Attributes.ToString().IndexOf("System") > -1)
{
return;
}
foreach(FileInfo file in folders.GetFiles())
{
//模糊查询(判断文件是否包含查询名)
if(file.IndexOf(FileName) > -1)
{
listBox1.Items.Add(file.FullName);
}
//精确查询(判断文件是否为查询名)
if(file.Name == FileName)
{
listBox1.Items.Add(file.FullName);
}
}
foreach(DirectoryInfo folder in folders.GetDirectories())
{
GetFileName(folder.FullName,FileName);
}
}
调用方法
string DirName = "D:/";
string FileName = textBox1.Text;
GetFileName(DirName,FileName);
定义方法
DirectoryInfo folders = new DirectoryInfo(DirName);
FileInfo[] files = folders.GetFiles();
for(int i = 0;i<files.Length;i++)
{
if(files[i].Name ==FileName)
{
listBox1.Items.Add("files[i].FullName");
}
}
DirectoryInfo[] folder = folders.GetDirectories();
for(j = 0;j<folder.Length;j++)
{
GetFileName(folder[j].FullName,FileName);
}