新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:遍历所有当前及下级目录,并记录为XML文件

遍历所有当前及下级目录,并记录为XML文件

新客网 XKER.COM 2004-11-05 来源: 收藏本文
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
const string mypath=@"D:\nba2004";
XmlDocument doc=new XmlDocument();

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 232);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click1);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}




private void button1_Click1(object sender, System.EventArgs e)
{

string MyPath=mypath.ToUpper();
doc.LoadXml("<FileInfo Name='"+MyPath+"'></FileInfo>");
GetFileInfo(MyPath,doc.DocumentElement);
doc.Save(@"d:\fileinfo.xml");
}
private void GetFileInfo(string strpath,System.Xml.XmlNode node)
{
DirectoryInfo DirInfo=new DirectoryInfo(strpath);
foreach(DirectoryInfo dirinfo in DirInfo.GetDirectories())
{
XmlElement elem = doc.CreateElement("dir");
elem.SetAttribute("Name",dirinfo.Name);
elem.SetAttribute("LastWriteTime",dirinfo.LastWriteTime.ToString());
node.AppendChild(elem);
GetFileInfo(dirinfo.FullName,elem);
}

foreach(FileInfo fileinfo in DirInfo.GetFiles())
{
XmlElement elem = doc.CreateElement("file");
elem.SetAttribute("Name",fileinfo.Name);
elem.SetAttribute("LastWriteTime",fileinfo.LastWriteTime.ToString());
elem.SetAttribute("Length",fileinfo.Length.ToString());
node.AppendChild(elem);
}
}

}

}



收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐