110,565
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
DirectoryInfo info = new DirectoryInfo("C:\\MSDOS622");
FindFile(info);
Console.ReadLine();
}
static void FindFile(DirectoryInfo d)
{
if (File.Exists("files.xml"))
{
XDocument doc = XDocument.Load("files.xml");
if (doc != null)
{
XElement XRoot = doc.Root;
if (XRoot != null)
{
string sDirName = d.FullName;
if (sDirName.StartsWith("C:\\") && XRoot.Element("DriverC")!=null)
{
XElement xlemDriverC = XRoot.Element("DriverC");
if (sDirName.Length <= 3)
{
FindFile(xlemDriverC);
}
else
{
foreach (XElement xlem in xlemDriverC.Elements("Dir"))
{
if (xlem.Attribute("DirName") != null && xlem.Attribute("DirName").Value == d.Name)
{
FindFile(xlem);
break;
}
}
}
}
}
}
}
}
static void FindFile(XElement xlemDir) //递归遍历子节点
{
foreach (XElement xlem in xlemDir.Elements())
{
if (xlem.Name == "File")
{
if (xlem.Attribute("FileName") != null)
Console.WriteLine(xlem.Attribute("FileName").Value);
}
else
{
FindFile(xlem);
}
}
}
}
你把那个XML文件放在执行文件同一路径下,就可以测了。void FindFile( Directory d )
{
FileOrFolders = d.GetFileOrFolders();
foreach( FileOrFolder fof in FileOrFolders )
{
if( fof is File )
You Found a file;
else if ( fof is Directory )
FindFile( fof );
}
}