This example demonstrates how to use recursive methods to copy all files (including subfolders) in a specified folder to a specified location.
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Namespace temp3
{
///
// Summary of webform1.
//
public class webform1: system. web. UI. page
{< br> protected system. web. UI. webcontrols. button button1;
private void page_load (Object sender, system. eventargs e)
{< br> // place the user Code here to initialize the page
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void button#click (Object sender, system. eventargs E)
{
Copyfiles (@ "D: \ PCT", @ "E: \ PCT ");
}
Private void copyfiles (string varfromdirectory, string vartodirectory)
{
Directory. createdirectory (vartodirectory );
If (! Directory. exists (varfromdirectory) return;
String [] directories = directory. getdirectories (varfromdirectory );
If (directories. length> 0)
{
Foreach (string D in directories)
{
Copyfiles (D, vartodirectory + D. substring (D. lastindexof ("\\")));
}
}
String [] files = directory. getfiles (varfromdirectory );
If (files. length> 0)
{
Foreach (string s in files)
{
File. Copy (S, vartodirectory + S. substring (S. lastindexof ("\\")));
}
}
}
}
}