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;
}