Copy the content of a Directory
<%@ Page language="c#" %>
<HTML>
<script language="c#" runat="server">
private void Copy_Click(System.Object sender, System.EventArgs e)
{
string[] arrSourcePath = RegEx.Split(tbDirectorySorgente.Text, "/");
string lastFolder = arrSourcePath(UBound(arrSourcePath));
string sourcePath = Server.MapPath(tbDirectorySorgente.Text);
string destPath = Server.MapPath(tbDirectoryDestinazione.Text + "/" + lastFolder);
try
{
CopyDirectory(sourcePath, destPath, True);
}
catch (Exception exc)
{
lbMessaggio.Text = exc.Message;
lbMessaggio.Visible = True;
}
}
private void CopyDirectory(string sourcePath, string destPath, bool overwrite)
{
System.IO.DirectoryInfo sourceDir = new System.IO.DirectoryInfo(sourcePath);
System.IO.DirectoryInfo destDir = new System.IO.DirectoryInfo(destPath);
if (sourceDir.Exists)
{
if (!destDir.Exists)
destDir.Create();
foreach (System.IO.FileInfo file in sourceDir.GetFiles())
{
if (overwrite)
file.CopyTo(System.IO.Path.Combine(destDir.FullName, file.Name), True);
else if ((System.IO.File.Exists(System.IO.Path.Combine(destDir.FullName, file.Name))) == false)
file.CopyTo(System.IO.Path.Combine(destDir.FullName, file.Name), false);
}
foreach (System.IO.DirectoryInfo dir in sourceDir.GetDirectories())
{
CopyDirectory(dir.FullName, System.IO.Path.Combine(destDir.FullName, dir.Name), overwrite);
}
lbMessaggio.Text = "Cartella copiata";
lbMessaggio.Visible = true;
}
else
{
lbMessaggio.Text = "La cartela non esiste!";
lbMessaggio.Visible = true;
}
}
</script>
<body>
<form runat="server" ID="Form1">
<table border="0" cellpadding="3" cellspacing="2">
<tr>
<td>Sorgente:</td>
<td><asp:TextBox Runat="server" ID="tbDirectorySorgente"></asp:TextBox></td>
</tr>
<tr>
<td>Destinazione:</td>
<td><asp:TextBox Runat="server" ID="tbDirectoryDestinazione"></asp:TextBox></td>
</tr>
<tr>
<TD> </TD>
<td colspan="2"><asp:Button Runat="server" ID="btnCopy" OnClick="Copy_Click" Text="CopiaCartella"></asp:Button></td>
</tr>
</table>
<br>
<asp:Label Runat="server" ID="lbMessaggio" Visible="False" ForeColor="#ff0000"></asp:Label>
</form>
</body>
</HTML>
Download Code...
Print Page