Obtains information about a folder and generates an XML file, which is displayed in the tree.

Source: Internet
Author: User

Because the company wants to prepare a software for online upgrading of servers and clients, it needs to obtain the file path information on the server and generate the corresponding XML file for a small software test, we have done it today ~~

Useful friends can learn from, mainlyCodeAs follows:

 

Private Xmldocument xmldoc;

Private   Void Btncreatexml_click ( Object Sender, eventargs E)
{
// Custom file path
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 );
// Loop traversal,Transferring the current node is the key, because it has been around for a long time.
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");
}
}

// Traverse the folder in the given path and create a node in the current 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 );
// Loop Traversal
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 );
}
}
}

// Obtains the size of a folder. Here, we use other people's code.
Private   Long Getdirectsize ( String Filepath)
{
Long Temp =   0 ;
If (File. exists (filepath) =   False ) // Determines whether the current path points to a file
{
String [] Str1 = Directory. getfilesystementries (filepath );
Foreach ( String S1 In Str1)
{
Temp+ =Getdirectsize (S1 );
}
}
Else
{< br> // defines a fileinfo object to associate it with the file directed by filepath, to obtain its size
fileinfo = New fileinfo (filepath );
return fileinfo. length;
}
Return Temp;
}

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.