Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

System.IO Console App, Copy Folder

Posted on 2008-06-10
4
Medium Priority
?
509 Views
Last Modified: 2013-12-17
I want to create a small console app that copys all files from one folder to another, but deletes all files in the destination folder first.

Thanks
string[] files = Directory.GetFiles(@"C:\temp");
            string[] sfiles = Directory.GetFiles(@"C:\temp1");
 
            foreach (string file in files)
                File.Delete(file);
 
            foreach (string sfile in sfiles)
                File.Copy(sfile, "@c:\temp");

Open in new window

0
Comment
Question by:martgriff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 21

Assisted Solution

by:mastoo
mastoo earned 400 total points
ID: 21751318
string[] files = Directory.GetFiles(@"C:\temp");

            string[] sfiles = Directory.GetFiles(@"C:\temp1");
 
            foreach (string file in files)
{
                File.Copy(sfile, "c:\\temp");
              File.Delete(file);
}
0
 
LVL 18

Accepted Solution

by:
Joel Coehoorn earned 800 total points
ID: 21751403

void CopyFolder(string srcPath, string destPath)
{
    string[] files = Directory.GetFiles(destPath);
    foreach (string file in files)
    {
        File.Delete(file);
    }
 
    if (!destPath.EndsWith(@"\")) destPath += @"\";
 
    files = Directory.GetFiles(srcPath);
    foreach (string file in files)
    {
       FileInfo theFile = new FileInfo(file);
       File.Copy(file, destPath + theFile.Name);
    }
}

Open in new window

0
 
LVL 12

Assisted Solution

by:jandromeda
jandromeda earned 800 total points
ID: 21752200
Here you go.
DirectoryInfo source = new DirectoryInfo(@"C:\My_Documents\My_Projects\DotNET\ExpertExchange\BkSource");
			DirectoryInfo dest = new DirectoryInfo(@"C:\My_Documents\My_Projects\DotNET\ExpertExchange\BkTarget");
			
			FileInfo[] files = dest.GetFiles();
			
			// Copy files in the destination folder
			foreach (FileInfo file in files)
			{
				file.Delete();
			}
			
			files = source.GetFiles();
			
			// Copy all the files in source folder to destination folder
			foreach (FileInfo file in files)
			{
				file.CopyTo(Path.Combine(dest.FullName, file.Name), true);
			}

Open in new window

0
 

Author Closing Comment

by:martgriff
ID: 31465718
Thanks for you help, I dod manage to sort this.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

647 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question