获取某个文件夹信息,并生成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;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值