如何获取一个目录下包含子目录内的所有文件名,并保存在一个数组中或XML文档里...

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 

///    

                  ///   将指定目录下的子目录和文件生成xml文档 

                  ///    

                  ///    根目录  

                  ///    返回XmlDocument对象  

                  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; 

                  } 

                  ///    

                  ///   生成Xml分支 

                  ///    

                  ///    子目录  

                  ///    父目录XmlDocument  

                  ///    XmlDocument对象  

                  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); 

                            } 

                  }               

          } 

        

}

你可能感兴趣的:(如何获取一个目录下包含子目录内的所有文件名,并保存在一个数组中或XML文档里...)