zoukankan      html  css  js  c++  java
  • 获取某个文件夹信息,并生成XML文件,按树形显示

    因为公司要做一个服务器与客户端在线升级的软件,需要用到获取服务器上文件路径的信息,并生成对应的XML文件做了一个小软件进行测试,今天总算搞定~~

    能用到的朋友可以借鉴,主要代码如下:

     

            private XmlDocument xmlDoc;

            
    private void btnCreateXml_Click(object sender, EventArgs e)
            
    {
                
    //自定义文件路径
                string strPath = "F:\\work2\\bkjj\\";

                xmlDoc 
    = new XmlDocument();
                xmlDoc.LoadXml(
    "<Directory></Directory>");

                
    try
                
    {
                    DirectoryInfo d1 
    = new DirectoryInfo(strPath);
                    FileSystemInfo[] fsi 
    = d1.GetFileSystemInfos();
         
                    
    foreach (FileSystemInfo fs in fsi)
                    
    {
                        XmlNode xn 
    = xmlDoc.SelectSingleNode("Directory");
                        
    if (fs is DirectoryInfo)
                        
    {
                            
                            XmlElement xe 
    = xmlDoc.CreateElement("Directory");
                            XmlAttribute xa1 
    = xmlDoc.CreateAttribute("Path");
                            XmlAttribute xa2 
    = xmlDoc.CreateAttribute("Size");
                            xa1.Value 
    = fs.FullName;
                            xa2.Value 
    = GetDirectSize(fs.FullName).ToString();
                            xe.Attributes.Append(xa1);
                            xe.Attributes.Append(xa2);
                            xn.AppendChild(xe);
                            
    //循环遍历,传递当前的节点是关键,因为这个原因搞了大半天
                            LoopDirectory(fs.FullName, xe);
                        }

                        
    else
                        
    {
                            FileInfo f1 
    = new FileInfo(fs.FullName);

                            XmlElement xe 
    = xmlDoc.CreateElement("File");
                            XmlAttribute xa1 
    = xmlDoc.CreateAttribute("Path");
                            XmlAttribute xa2 
    = xmlDoc.CreateAttribute("Size");
                            xa1.Value 
    = fs.FullName;
                            xa2.Value 
    = f1.Length.ToString();
                            xe.Attributes.Append(xa1);
                            xe.Attributes.Append(xa2);

                            xn.AppendChild(xe);

                        }

                    }

                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(ex.Message);
                }

                
    finally
                
    {
                    xmlDoc.Save(
    "fileInfo.xml");
                }

            }


            
    //遍历所给路径的文件夹,并在当前的XmlElement处创建节点
            private void LoopDirectory(string strPath,XmlElement ele)
            
    {
                DirectoryInfo d1 
    = new DirectoryInfo(strPath);
                FileSystemInfo[] fsi 
    = d1.GetFileSystemInfos();
                
    foreach (FileSystemInfo fs in fsi)
                
    {
                    
    if (fs is DirectoryInfo)
                    
    {
                        XmlElement xe 
    = xmlDoc.CreateElement("Directory");
                        XmlAttribute xa1 
    = xmlDoc.CreateAttribute("Path");
                        XmlAttribute xa2 
    = xmlDoc.CreateAttribute("Size");
                        xa1.Value 
    = fs.FullName;
                        xa2.Value 
    = GetDirectSize(fs.FullName).ToString();
                        xe.Attributes.Append(xa1);
                        xe.Attributes.Append(xa2);

                        ele.AppendChild(xe);
                        
    //循环遍历
                        LoopDirectory(fs.FullName,xe);
                    }

                    
    else
                    
    {
                        FileInfo f1 
    = new FileInfo(fs.FullName);
                        XmlElement xe 
    = xmlDoc.CreateElement("File");
                        XmlAttribute xa1 
    = xmlDoc.CreateAttribute("Path");
                        XmlAttribute xa2 
    = xmlDoc.CreateAttribute("Size");
                        xa1.Value 
    = fs.FullName;
                        xa2.Value 
    = f1.Length.ToString();
                        xe.Attributes.Append(xa1);
                        xe.Attributes.Append(xa2);

                        ele.AppendChild(xe);
                    }

                }

            }


            
    //获取一个文件夹的大小,这里借鉴别人的代码
            private long GetDirectSize(string filePath)
            
    {
                
    long temp = 0;
                
    if (File.Exists(filePath) == false)//判断当前路径所指向的是否为文件
                {
                    
    string[] str1 = Directory.GetFileSystemEntries(filePath);
                    
    foreach (string s1 in str1)
                    
    {
                        temp 
    += GetDirectSize(s1);
                    }

                }

                
    else
                
    {
                    
    //定义一个FileInfo对象,使之与filePath所指向的文件向关联,以获取其大小
                    FileInfo fileInfo = new FileInfo(filePath);
                    
    return fileInfo.Length;
                }

                
    return temp;
            }
  • 相关阅读:
    现实世界的Windows Azure:采访Gridsum的Sr.业务发展总监Yun Xu
    现在可用——Windows Azure SDK 1.6
    Rock Paper Azure Challenge回来啦
    这几天比较忙,自己的职业生涯规划好了吗?目标又是什么呢?生活在十字路口。。。。。。
    GDI+ 学习记录(24): 输出文本<3>
    GDI+ 学习记录(30): MetaFile 文件操作
    GDI+ 学习记录(29): 区域 Region
    GDI+ 学习记录(26): 显示图像 Image
    GDI+ 学习记录(25): 变换 Transform
    返回整数的四种情况
  • 原文地址:https://www.cnblogs.com/cyan/p/1390061.html
Copyright © 2011-2022 走看看