Solved

# Move All Files in a directory

Posted on 2011-10-24
152 Views
Using C#, how would I copy out all files in a directory and move to another folder?
0
Question by:carsRST

LVL 39

Expert Comment

0

LVL 74

Accepted Solution

It could be as simple as:

using System.IO;

namespace _27412501
{
class Program
{
static void Main(string[] args)
{
string targetDir = @"C:\path\to\new\folder";
DirectoryInfo folder = new DirectoryInfo(@"C:\path\to\folder");

foreach (FileInfo file in folder.GetFiles())
{
file.CopyTo(Path.Combine(targetDir, file.Name));
}
}
}
}

0

LVL 74

Expert Comment

If you want to actually move them, then you can use MoveTo instead of CopyTo.
0

LVL 16

Author Closing Comment

thanks again!
0

LVL 16

Author Comment

>>If you want to actually move them, then you can use MoveTo instead of CopyTo.

thx
0

## Featured Post

### Suggested Solutions

Submitting to a REST API via a CLR in C# 12 49
c# vs2013 corrupt .dll 4 34
string format question 15 39
Asp.net mvc 2 30
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…