热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

获取某个文件夹信息,并生成XML文件,按树形显示

发布时间:2010年03月04日点击数: 佚名

因为公司要做一个服务器与客户端在线升级的软件,需要用到获取服务器上文件路径的信息,并生成对应的XML文件做了一个小软件进行测试,能用到的朋友可以借鉴,主要代码如下:

  1. private XmlDocument xmlDoc; 
  2.  
  3.         private void btnCreateXml_Click(object sender, EventArgs e) 
  4.         { 
  5.             //自定义文件路径 
  6.             string strPath = "F:\\work2\\bkjj\\"
  7.  
  8.             xmlDoc = new XmlDocument(); 
  9.             xmlDoc.LoadXml("<Directory></Directory>"); 
  10.  
  11.             try 
  12.             { 
  13.                 DirectoryInfo d1 = new DirectoryInfo(strPath); 
  14.                 FileSystemInfo[] fsi = d1.GetFileSystemInfos(); 
  15.       
  16.                 foreach (FileSystemInfo fs in fsi) 
  17.                 { 
  18.                     XmlNode xn = xmlDoc.SelectSingleNode("Directory"); 
  19.                     if (fs is DirectoryInfo) 
  20.                     { 
  21.                          
  22.                         XmlElement xe = xmlDoc.CreateElement("Directory"); 
  23.                         XmlAttribute xa1 = xmlDoc.CreateAttribute("Path"); 
  24.                         XmlAttribute xa2 = xmlDoc.CreateAttribute("Size"); 
  25.                         xa1.Value = fs.FullName; 
  26.                         xa2.Value = GetDirectSize(fs.FullName).ToString(); 
  27.                         xe.Attributes.Append(xa1); 
  28.                         xe.Attributes.Append(xa2); 
  29.                         xn.AppendChild(xe); 
  30.                         //循环遍历,传递当前的节点是关键,因为这个原因搞了大半天 
  31.                         LoopDirectory(fs.FullName, xe); 
  32.                     } 
  33.                     else 
  34.                     { 
  35.                         FileInfo f1 = new FileInfo(fs.FullName); 
  36.  
  37.                         XmlElement xe = xmlDoc.CreateElement("File"); 
  38.                         XmlAttribute xa1 = xmlDoc.CreateAttribute("Path"); 
  39.                         XmlAttribute xa2 = xmlDoc.CreateAttribute("Size"); 
  40.                         xa1.Value = fs.FullName; 
  41.                         xa2.Value = f1.Length.ToString(); 
  42.                         xe.Attributes.Append(xa1); 
  43.                         xe.Attributes.Append(xa2); 
  44.  
  45.                         xn.AppendChild(xe); 
  46.  
  47.                     } 
  48.                 } 
  49.             } 
  50.             catch (Exception ex) 
  51.             { 
  52.                 MessageBox.Show(ex.Message); 
  53.             } 
  54.             finally 
  55.             { 
  56.                 xmlDoc.Save("fileInfo.xml"); 
  57.             } 
  58.         } 
  59.  
  60.         //遍历所给路径的文件夹,并在当前的XmlElement处创建节点 
  61.         private void LoopDirectory(string strPath,XmlElement ele) 
  62.         { 
  63.             DirectoryInfo d1 = new DirectoryInfo(strPath); 
  64.             FileSystemInfo[] fsi = d1.GetFileSystemInfos(); 
  65.             foreach (FileSystemInfo fs in fsi) 
  66.             { 
  67.                 if (fs is DirectoryInfo) 
  68.                 { 
  69.                     XmlElement xe = xmlDoc.CreateElement("Directory"); 
  70.                     XmlAttribute xa1 = xmlDoc.CreateAttribute("Path"); 
  71.                     XmlAttribute xa2 = xmlDoc.CreateAttribute("Size"); 
  72.                     xa1.Value = fs.FullName; 
  73.                     xa2.Value = GetDirectSize(fs.FullName).ToString(); 
  74.                     xe.Attributes.Append(xa1); 
  75.                     xe.Attributes.Append(xa2); 
  76.  
  77.                     ele.AppendChild(xe); 
  78.                     //循环遍历 
  79.                     LoopDirectory(fs.FullName,xe); 
  80.                 } 
  81.                 else 
  82.                 { 
  83.                     FileInfo f1 = new FileInfo(fs.FullName); 
  84.                     XmlElement xe = xmlDoc.CreateElement("File"); 
  85.                     XmlAttribute xa1 = xmlDoc.CreateAttribute("Path"); 
  86.                     XmlAttribute xa2 = xmlDoc.CreateAttribute("Size"); 
  87.                     xa1.Value = fs.FullName; 
  88.                     xa2.Value = f1.Length.ToString(); 
  89.                     xe.Attributes.Append(xa1); 
  90.                     xe.Attributes.Append(xa2); 
  91.  
  92.                     ele.AppendChild(xe); 
  93.                 } 
  94.             } 
  95.         } 
  96.  
  97.         //获取一个文件夹的大小,这里借鉴别人的代码 
  98.         private long GetDirectSize(string filePath) 
  99.         { 
  100.             long temp = 0; 
  101.             if (File.Exists(filePath) == false)//判断当前路径所指向的是否为文件 
  102.             { 
  103.                 string[] str1 = Directory.GetFileSystemEntries(filePath); 
  104.                 foreach (string s1 in str1) 
  105.                 { 
  106.                     temp += GetDirectSize(s1); 
  107.                 } 
  108.             } 
  109.             else 
  110.             { 
  111.                 //定义一个FileInfo对象,使之与filePath所指向的文件向关联,以获取其大小 
  112.                 FileInfo fileInfo = new FileInfo(filePath); 
  113.                 return fileInfo.Length; 
  114.             } 
  115.             return temp; 
  116.         } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作