Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:29:28+00:00 2026-05-24T16:29:28+00:00

I found a small snippet for doing a recursive file copy in C#, but

  • 0

I found a small snippet for doing a recursive file copy in C#, but am somewhat stumped. I basically need to copy a directory structure to another location, along the lines of this…

Source: C:\data\servers\mc

Target: E:\mc

The code for my copy function as of right now is…

    //Now Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(baseDir, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(baseDir, targetDir));
    }


    // Copy each file into it’s new directory.
    foreach (string file in Directory.GetFiles(baseDir, "*.*", SearchOption.AllDirectories))
    {
        Console.WriteLine(@"Copying {0}\{1}", targetDir, Path.GetFileName(file));
        if (!CopyFile(file, Path.Combine(targetDir, Path.GetFileName(file)), false))
        {
            int err = Marshal.GetLastWin32Error();
            Console.WriteLine("[ERROR] CopyFile Failed on {0} with code {1}", file, err);
        }
    }

The issue is that in the second scope, I either:

  1. use Path.GetFileName(file) to get the actual file name without the path but I lose the directory “mc” directory structure or
  2. use “file” without Path.Combine.

Either way I have to do some nasty string work. Is there a good way to do this in C# (my lack of knowledge with the .NET API leads me to over complicating things)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:29:29+00:00Added an answer on May 24, 2026 at 4:29 pm

    instead of

    foreach (string file in Directory.GetFiles(baseDir, "*.*", SearchOption.AllDirectories))
    {
    

    do something like this

    foreach (FileInfo fi in source.GetFiles())
    {
         fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a small snippet of code that I found. when we make a
I found the Exploding Ring particle effect best suite what I need but I
I've found this small code that sends email to gmail users. I'd like the
I'm trying to grasp the concept of continuations and I found several small teaching
I have an small question I didn't found an answer yet: how do I
This is from a small library that I found online: const char* GetHandStateBrief(const PostFlopState*
I've had a new found interest in building a small, efficient web server in
I found this css body { font: 13px/1.231 arial,helvetica,clean,sans-serif; *font-size: small; *font: x-small; }
I have found that my HTML is, to be honest, very clunky. Small, simple
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.