Move All Files in a directory

Posted on 2011-10-24
Last Modified: 2012-05-12
Using C#, how would I copy out all files in a directory and move to another folder?
Question by:carsRST
    LVL 39

    Expert Comment

    by:Kyle Abrahams
    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));

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    If you want to actually move them, then you can use MoveTo instead of CopyTo.
    LVL 16

    Author Closing Comment

    thanks again!
    LVL 16

    Author Comment

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    Submitting to a REST API via a CLR in C# 12 49
    c# vs2013 corrupt .dll 4 34
    string format question 15 39 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…

    779 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

    18 Experts available now in Live!

    Get 1:1 Help Now