Traverse all current and subordinate directories and log as XML files

Source: Internet
Author: User
Tags add foreach log string tostring
xml| traversal

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>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.Button button1;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Const string mypath=@ "D:\nba2004";
XmlDocument doc=new XmlDocument ();

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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>
The main entry point for the application.
</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);
}
}

}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.