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 8952953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:59:17+00:00 2026-06-15T13:59:17+00:00

Hi I am trying to write a simple program to copy a folder from

  • 0

Hi I am trying to write a simple program to copy a folder from one soure to many in parallel.
I am learning c# so have been trying to understand and change code examples, as i figured this the best way to learn somthing new.

The example below does not work as it only copies to the first destination in the destinationPaths

The stange thing is i have a simlar method to copy one file to many and this works everytime
have i missing something?? i would be greatful if someone could tell me why this is not working i am guessing that there maybe certain things you can’t do in parallel

any advice would be great

public  void CopyMultipleFolder(string sourceFilePath, params string[] destinationPaths)
    {
        if (string.IsNullOrEmpty(sourceFilePath)) MessageBox.Show("A source file must be specified.", "sourceFilePath");
        else
        {

            if (destinationPaths == null || destinationPaths.Length == 0) MessageBox.Show("At least one destination file must be specified.", "destinationPaths");
            else
            {
                try
                {



                    FileIOPermission writeAccess = new FileIOPermission(FileIOPermissionAccess.AllAccess, destinationPaths);
                    foreach (string i in destinationPaths)
                    {
                        writeAccess.AddPathList(FileIOPermissionAccess.Write, i);
                    }

                    writeAccess.Demand();






                    NetworkCredential user = new NetworkCredential();
                    user.UserName = Properties.Settings.Default.username;
                    user.Password = Properties.Settings.Default.password;

                    if (user.Password.Length == 0 || user.UserName.Length == 0)
                    {
                        MessageBox.Show("No Username or password have been entered click username on menu bar to update", "Update Credentials");
                    }
                    else
                    {

                        Parallel.ForEach(destinationPaths, new ParallelOptions(),
                                         destinationPath =>
                                         {


                                             if (sourceFilePath.EndsWith("*"))
                                             {
                                                 int l = sourceFilePath.Length - 4;

                                                 sourceFilePath = sourceFilePath.Remove(l);
                                             }
                                             else
                                             {

                                                 using (new NetworkConnection(destinationPath, user))
                                                 {
                                                     if (Directory.Exists(destinationPath + "\\" + foldername))
                                                     {
                                                         if (destinationPath.EndsWith("\\"))
                                                         {
                                                             DialogResult r = MessageBox.Show("Folder already Exists " + destinationPath + foldername + " Do You Want To overwrite All Files And Sub Folders", "Overwrite?", MessageBoxButtons.YesNo);


                                                             if (r == DialogResult.Yes)
                                                             {
                                                                 PleaseWait.Create();

                                                                 foreach (string dirPath in Directory.GetDirectories(sourceFilePath, "*", SearchOption.AllDirectories))
                                                                     Directory.CreateDirectory(dirPath.Replace(sourceFilePath, destinationPath + "\\" + foldername));


                                                                 foreach (string newPath in Directory.GetFiles(sourceFilePath, "*.*", SearchOption.AllDirectories))
                                                                     File.Copy(newPath, newPath.Replace(sourceFilePath, destinationPath+ "\\" + foldername), true);


                                                                 list = list + destinationPath + foldername + Environment.NewLine;
                                                             }
                                                             else
                                                             {
                                                             }

                                                         }
                                                         else
                                                         {


                                                             DialogResult r = MessageBox.Show("Folder already Exists " + destinationPath + "\\" + foldername + " Do you Want to overwrite All Files And SubFolders", "Overwrite?", MessageBoxButtons.YesNo);

                                                             if (r == DialogResult.Yes)
                                                             {
                                                                 PleaseWait.Create();

                                                                 foreach (string dirPath in Directory.GetDirectories(sourceFilePath, "*", SearchOption.AllDirectories))
                                                                     Directory.CreateDirectory(dirPath.Replace(sourceFilePath, destinationPath + "\\" + foldername));

                                                                 //Copy all the files
                                                                 foreach (string newPath in Directory.GetFiles(sourceFilePath, "*.*", SearchOption.AllDirectories))
                                                                     File.Copy(newPath, newPath.Replace(sourceFilePath, destinationPath + "\\" + foldername), true);


                                                                 list = list + destinationPath + "\\" + foldername + Environment.NewLine;
                                                             }
                                                             else
                                                             {
                                                             }
                                                         }
                                                     }
                                                     else
                                                     {
                                                         PleaseWait.Create();

                                                         foreach (string dirPath in Directory.GetDirectories(sourceFilePath, "*", SearchOption.AllDirectories))
                                                             Directory.CreateDirectory(dirPath.Replace(sourceFilePath, destinationPath + "\\" + foldername));

                                                         //Copy all the files
                                                         foreach (string newPath in Directory.GetFiles(sourceFilePath, "*.*", SearchOption.AllDirectories))
                                                             File.Copy(newPath, newPath.Replace(sourceFilePath, destinationPath + "\\" + foldername), true);


                                                         list = list + destinationPath +"\\"+foldername+ Environment.NewLine;
                                                     }
                                                 }



                                             }
                                             PleaseWait.Destroy();
                                         });


                        MessageBox.Show("Folder Has Been Copied to " + list, "Folder Copied");
                    }
                }

                catch (UnauthorizedAccessException uae)
                {
                    MessageBox.Show(uae.ToString());
                }



            }
        }
    }
  • 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-06-15T13:59:18+00:00Added an answer on June 15, 2026 at 1:59 pm

    You wrote you were learning C#. So, forget about parallel execution, because it unnecessarily makes your task more complicated. Instead, start by decomposing your problem into smaller parts. The code you posted is ugly, long, repeats a lot of logic many times, and hence it is and will be hard to read, debug, and maintain.

    So, start by writing small functions for individual files. You need to create a set of folders in a destination folder. Hence write a function accepting a list of names and the destination folder. You need to determine the set of folders from a source folder. So write a function which does that. The combine those two functions together. And so on.

    You will end up with a much cleaner, modifiable, reusable solution. Then it will be a lot easier to plug in parallel processing. Most likely, this will be for the sake of learning it, because it makes not much sense to parallelize your problem too heavily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple stock check program, and I have a Table
I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib.
I'm trying to write a simple program to monitor a folder for new files
I am trying to write a simple program that reads integers from a data
I am trying to write a simple Ruby program that I will run from
I am trying to write a simple program. It's supposed to read links from
I am trying to write a simple program in C# that will read data
I'm trying to write a simple program that takes in the users input, then
I am trying to write a simple program to open a socket channel to
I'm new to Pascal and I am trying to write a simple program, 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.