private void getDirectories(string path) { string[] fileNames = Directory.GetFiles(path); string[] directories = Directory.GetDirectories(path); foreach (string file in fileNames) { Console.WriteLine( "Filename:{0} ",file); } foreach (string dir in directories) { Console.WriteLine( "Directoriesname:{0} ",dir); getDirectories(dir); } } Console.WriteLine这个地方你可以换成arraylist
或
可以参考我以前写的一个类,生成xml文件。 http://blog.csdn.net/zhzuo/archive/2005/03/08/315125.aspx /// <summary> /// 将指定目录下的子目录和文件生成xml文档 /// </summary> /// <param name= "targetDir "> 根目录 </param> /// <returns> 返回XmlDocument对象 </returns> public static XmlDocument CreateXml(string targetDir) { XmlDocument myDocument = new XmlDocument(); XmlDeclaration declaration = myDocument.CreateXmlDeclaration( "1.0 ", "utf-8 ",null); myDocument.AppendChild(declaration); XmlElement rootElement = myDocument.CreateElement(targetDir.Substring(targetDir.LastIndexOf(PATH_SPLIT_CHAR) + 1)); myDocument.AppendChild(rootElement); foreach(string fileName in Directory.GetFiles(targetDir)) { XmlElement childElement = myDocument.CreateElement( "File "); childElement.InnerText = fileName.Substring(fileName.LastIndexOf(PATH_SPLIT_CHAR) + 1); rootElement.AppendChild(childElement); } foreach(string directory in Directory.GetDirectories(targetDir)) { XmlElement childElement = myDocument.CreateElement( "Directory "); childElement.SetAttribute( "Name ",directory.Substring(directory.LastIndexOf(PATH_SPLIT_CHAR) + 1)); rootElement.AppendChild(childElement); CreateBranch(directory,childElement,myDocument); } return myDocument; } /// <summary> /// 生成Xml分支 /// </summary> /// <param name= "targetDir "> 子目录 </param> /// <param name= "xmlNode "> 父目录XmlDocument </param> /// <param name= "myDocument "> XmlDocument对象 </param> private static void CreateBranch(string targetDir,XmlElement xmlNode,XmlDocument myDocument) { foreach(string fileName in Directory.GetFiles(targetDir)) { XmlElement childElement = myDocument.CreateElement( "File "); childElement.InnerText = fileName.Substring(fileName.LastIndexOf(PATH_SPLIT_CHAR)+1); xmlNode.AppendChild(childElement); } foreach(string directory in Directory.GetDirectories(targetDir)) { XmlElement childElement = myDocument.CreateElement( "Directory "); childElement.SetAttribute( "Name ",directory.Substring(directory.LastIndexOf(PATH_SPLIT_CHAR) + 1)); xmlNode.AppendChild(childElement); CreateBranch(directory,childElement,myDocument); } } } }