Copying a Directory and Files
In this article I'm trying to explain how to copy Directory information from one path to another path and how to copy files from one directory to another directory. This will help you those who are beginners to perform this action.
Copying a Directory and Files:
In this article I'm trying to explain how to copy Directory from one path to another path and how to copy files from one directory to another directory.
Follow below steps to achieve your goal.Step-1:
Create a project and right click on solution explorer then Choose AddNewItem then choose webform and give a name for that page as "CopyDirectory.aspx".Step-2:
Once page has been created under project then in design view simply drag and drop 3 label controls, 2 textbox controls and 1 button control. Then the design view looks becomes like below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CopyDirectory.aspx.cs" Inherits="ASPnet_CopyDirectory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblsource" runat="server"
Text="Enter Path and name of Source Directory : "></asp:Label>
<asp:TextBox ID="txtsrc" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server"
Text="Enter Path and name of destination directory : "></asp:Label>
<asp:TextBox ID="txtdest" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnCopyDir" runat="server" onclick="btnCopyDir_Click"
Text="Copy Directory" />
<br />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
</div>
</form>
</body>
</html>Step-3:
Once design part has been completed then on click event of button wrote below lines of code.
protected void btnCopyDir_Click(object sender, EventArgs e)
{
try
{
System.IO.DirectoryInfo SrcDir = new System.IO.DirectoryInfo(txtsrc.Text);
System.IO.DirectoryInfo DestDir = new System.IO.DirectoryInfo(txtdest.Text);
CopyDir(SrcDir, DestDir);
lblResult.Text = " Directory Copied successfully...!!!";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}
protected void CopyDir(System.IO.DirectoryInfo src, System.IO.DirectoryInfo dest)
{
//check if the destination folder contains that directory or not.
if (!dest.Exists)
{
//if it's not created the directory then we create under destination folder
dest.Create();
// fetch available files from source directory folder.
System.IO.FileInfo[] files = src.GetFiles();
foreach (System.IO.FileInfo file in files)
{
//copy each and every file into destination folder
file.CopyTo(System.IO.Path.Combine(dest.FullName, file.Name));
}
//fetch available sub directories into that directory
System.IO.DirectoryInfo[] dirs = src.GetDirectories();
foreach (System.IO.DirectoryInfo dir in dirs)
{
string destDir = System.IO.Path.Combine(dest.FullName, dir.Name);
//rebind directory method again why because if subdirectory contains any files or again sub directories this method will perform that action again
CopyDir(dir, new System.IO.DirectoryInfo(destDir));
}
}
// if destination directory exists but under that files or not exists this part of code will help you to copy the files into that directory
else if (dest.Exists)
{
//fetch available files in that specified directory
System.IO.FileInfo[] files = src.GetFiles();
foreach (System.IO.FileInfo file in files)
{
string filenamechk=System.IO.Path.Combine(dest.FullName, file.Name);
var exists=System.IO.Directory.GetFiles(dest.FullName).Any(x=> x.Equals(filenamechk,StringComparison.OrdinalIgnoreCase));
//check if the specified file exists in that particular directory or not if not exists then copy that file into the destination directory
if(!exists)
{
file.CopyTo(System.IO.Path.Combine(dest.FullName, file.Name));
}
}
}
}Step-4:
Once total code part has been completed then executes that application by pressing f5 on keyboard and sees the result set in the screen.Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ASPnet_CopyDirectory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCopyDir_Click(object sender, EventArgs e)
{
try
{
System.IO.DirectoryInfo SrcDir = new System.IO.DirectoryInfo(txtsrc.Text);
System.IO.DirectoryInfo DestDir = new System.IO.DirectoryInfo(txtdest.Text);
CopyDir(SrcDir, DestDir);
lblResult.Text = "Directories and Files Copied successfully...!!!";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}
protected void CopyDir(System.IO.DirectoryInfo src, System.IO.DirectoryInfo dest)
{
if (!dest.Exists)
{
dest.Create();
System.IO.FileInfo[] files = src.GetFiles();
foreach (System.IO.FileInfo file in files)
{
file.CopyTo(System.IO.Path.Combine(dest.FullName, file.Name));
}
System.IO.DirectoryInfo[] dirs = src.GetDirectories();
foreach (System.IO.DirectoryInfo dir in dirs)
{
string destDir = System.IO.Path.Combine(dest.FullName, dir.Name);
CopyDir(dir, new System.IO.DirectoryInfo(destDir));
}
}
else if (dest.Exists)
{
System.IO.FileInfo[] files = src.GetFiles();
foreach (System.IO.FileInfo file in files)
{
string filenamechk=System.IO.Path.Combine(dest.FullName, file.Name);
var exists=System.IO.Directory.GetFiles(dest.FullName).Any(x=> x.Equals(filenamechk,StringComparison.OrdinalIgnoreCase));
if(!exists)
{
file.CopyTo(System.IO.Path.Combine(dest.FullName, file.Name));
}
}
}
}
}Conclusion:
This article will help you those who are looking for the same and those who are trying to copy one directory information into another directory.