• 如何获取一个目录下包含子目录内的所有文件名,并保存在一个数组中或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 
    
    ///   <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); 
    
                                } 
    
                      }               
    
              } 
    
            
    
    }
    
  • 相关阅读:
    ubuntu 14.04 firefox install flash-plugin
    ubuntu node.js Binaries方式安装(二进制文件安装)
    ubuntu14.04 截图
    ubuntu 14.04下,thinkpad触摸板关闭方法
    ubuntu Mozilla Firefox install flash plugin 火狐浏览器安装flash插件
    win7+ubuntu 14.04双系统 硬盘安装
    后台启动VirtualBox虚拟机
    excel vlookup函数使用方法
    图片添加文字水印和图片水印
    记录使用Stream转多层map数据结构及遇到的坑
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2119204.html
Copyright © 2020-2023  润新知