dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersUltimaterengan
Sameer Sayani
sabareesh reddy
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

How to move all files from one directory to other directory using C# code?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 25


In this article I am going to explain about how to move all files from the one directory to other using C# code. This concept is used in various scenario in our projects.



 


Description


I am create one method name as MoveFiles, this method is call recursively for sub folders files too. So move all files including sub folder files too using this concept. And then finally after move all files just delete that old directory using C# code itself.

Client side


Just I have placed one button control to get command from user.

Server side



public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string source, destination;
Boolean overwrite=false;

//Mention here your source and destination path
//source mean where you want copy file and destination means where you move to that file
source = "E:\\temp1";
destination = "E:\\temp2";
MoveFiles(source, destination, overwrite);
//Finally delete old folder after copy that files
if (System.IO.Directory.Exists(source))
{
System.IO.Directory.Delete(source);
}
}

private void MoveFiles(string source, string destination, bool overwrite)
{
System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo(source);
System.IO.DirectoryInfo outputDir = new System.IO.DirectoryInfo(destination);
try
{
if ((inputDir.Exists))
{
if (!(outputDir.Exists))
{
outputDir.Create();
}

//Get Each files and copy
System.IO.FileInfo file = null;
foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
{
file = eachfile;
if ((overwrite))
{
file.CopyTo(System.IO.Path.Combine(outputDir.FullName, file.Name), true);
}
else
{
if (((System.IO.File.Exists(System.IO.Path.Combine(outputDir.FullName, file.Name))) == false))
{
file.CopyTo(System.IO.Path.Combine(outputDir.FullName, file.Name), false);
}
}
System.IO.File.Delete(file.FullName);
}

//Sub folder access code

System.IO.DirectoryInfo dir = null;
foreach (System.IO.DirectoryInfo subfolderFile in inputDir.GetDirectories())
{
dir = subfolderFile;
//Destination path
if ((dir.FullName != outputDir.ToString()))
{
MoveFiles(dir.FullName, System.IO.Path.Combine(outputDir.FullName, dir.Name), overwrite);
}
System.IO.Directory.Delete(dir.FullName);
}
}
}
catch (Exception ex)
{
}
}
}

Source code:


Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this article is help you to know about Move all files from one to another directory.

Attachments
  • Source _code (44021-0440-Source-code.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to move all files from one directory to other directory using C# code?"
    Guest Author: rahul     28 Dec 2012
    tahnk u boss.really helped me...


    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: What is SCOPE_IDENTITY ? How to use SCOPE_IDENTITY?
    Previous Resource: How to filter only numeric values in the SQL SERVER table?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Move Files  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.