Solved

System.IO Console App, Copy Folder

Posted on 2008-06-10
4
495 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks for you help, I dod manage to sort this.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

744 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

16 Experts available now in Live!

Get 1:1 Help Now