Solved

System.IO Console App, Copy Folder

Posted on 2008-06-10
4
497 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
4 Comments
 
LVL 21

Assisted Solution

by:mastoo
mastoo earned 100 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:
jcoehoorn earned 200 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 200 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 51
C# application error "Parameter Is Not Valid". 3 75
SQL Login 17 38
Visual Studio 2015 Source Code Control 10 26
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now