C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Copying Directory


Posted Date: 24 Aug 2009    Resource Type: Code Snippets    Category: File Operations
Author: Ramesh N DMember Level: Gold    
Rating: 1 out of 5Points: 7



The Below code copies the directory and all its sub directories from source to destination.

You have to mention the source path and Destination Path, if source path exist, first it copies the files and then folders...Recursively it calls the folder... And this is also an example for Recusive function


public void CopyDirectory(string SourcePath, string DestinationPath)
{
try
{

if (Directory.Exists(SourcePath))
{
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo finfo = new FileInfo(fls);
finfo.CopyTo(DestinationPath + "\\" + finfo.Name.ToString());
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (!(Directory.Exists(DestinationPath + "\\" + drinfo.Name.ToString())))
{
Directory.CreateDirectory(DestinationPath + "\\" + drinfo.Name.ToString());
CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString);
}
else
{
CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString());
}

}

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}



Responses

Author: Miss Meetu Choudhary    27 Aug 2009Member Level: Diamond   Points : 2
Add Description To your Code...


Author: Ramesh N D    29 Aug 2009Member Level: Gold   Points : 2
Below code copies the directory and all its sub directory from source to destination.

you has to mention the source path and Destination Path, if source path exist, first it copies the files and go for floders...Recursively it calls the folder... And this is also an example for Recusive function

public void CopyDirectory(string SourcePath, string DestinationPath)
{
try
{

if (Directory.Exists(SourcePath))
{
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo finfo = new FileInfo(fls);
finfo.CopyTo(DestinationPath + "\\" + finfo.Name.ToString());
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (!(Directory.Exists(DestinationPath + "\\" + drinfo.Name.ToString())))
{
Directory.CreateDirectory(DestinationPath + "\\" + drinfo.Name.ToString());
CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString);
}
else
{
CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString());
}

}

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


}



Author: Ramesh N D    29 Aug 2009Member Level: Gold   Points : 2
Below code copies the directory and all its sub directory from source to destination.

you has to mention the source path and Destination Path, if source path exist, first it copies the files and go for floders...Recursively it calls the folder... And this is also an example for Recusive function

public void CopyDirectory(string SourcePath, string DestinationPath)
{
try
{

if (Directory.Exists(SourcePath))
{
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo finfo = new FileInfo(fls);
finfo.CopyTo(DestinationPath + "\\" + finfo.Name.ToString());
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (!(Directory.Exists(DestinationPath + "\\" + drinfo.Name.ToString())))
{
Directory.CreateDirectory(DestinationPath + "\\" + drinfo.Name.ToString());
CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString);
}
else
{
CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString());
}

}

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


}



Author: Miss Meetu Choudhary    30 Aug 2009Member Level: Diamond   Points : 2
PLease Format Your Resource. Don't add comments Edit This Resource


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Copying Directory  .  Copies the directory and all its sub directory and an example for Recusive function  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: File search without recursion
Previous Resource: Create resource file
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use