Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to copy and replace folder in desktop with C#/Winforms?

How to copy and replace folder in desktop with C#/Winforms?

Scheduled Pinned Locked Moved C#
csharpwinformstutorialquestionworkspace
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G George Tourtsinakis

    Hi guys, I want when i have a radio button checked by pressing a button the desired folder with all files in it to be in desktop and replacing anything. I have this :

    public partial class Form1 : Form
    {

        string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    

    and then :

    private void button1_Click(object sender, EventArgs e)
    {

            foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text))
            {
                File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true);
                if (YesRadioButton.Checked == true)
                {
                    File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true);
    
                }
    
            }
        }
    

    Thank you in advance.While the code works for every directory everywhere I can't do anything with the directory I want in desktop.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    octapuslabs wrote:

    I can't do anything with the directory I want in desktop.

    I am afraid that is not clear. Please explain exactly what problem you see at this point.

    G 1 Reply Last reply
    0
    • L Lost User

      octapuslabs wrote:

      I can't do anything with the directory I want in desktop.

      I am afraid that is not clear. Please explain exactly what problem you see at this point.

      G Offline
      G Offline
      George Tourtsinakis
      wrote on last edited by
      #3

      Let's say I have a directory c:\test with some files in it.I want this directory to be copied in desktop with every file in it.If it exists I want to replace it with the new files .I m making a program so I can have backup of my projects easily and not copying pasting with windows clicks all the time . This is the whole code which creates a folder in desktop Downloads\Downloads and also some files are copies to desktop\Downloads and some others to desktop\Downloads\Downloads.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using System.IO;//We need this namespace to be used so we can have the file class
      //that gives us the ability to copy,delte files etc.

      namespace EasyCopyFiles
      {
      public partial class Form1 : Form
      {

          string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
      
      
          public Form1()
          {
              InitializeComponent();
      
      
      
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
      
      
              foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text))//SourceOneToCopy
                  //textbox we read from user input exammple c:\\test
              {
                  //OutputDirectory1 also a text box we read from user example d:\\test
                  File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true);
      
                  if (YesRadioButton.Checked == true)
                  {
                      desktop += "\\\\Downloads";//With the \\\\ we create the path we want
                      if (!System.IO.Directory.Exists(desktop))
                      {
      
                          DirectoryInfo directory = Directory.CreateDirectory(desktop);
      
                      }
      
                      File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true);
      
      
                  }
      
              }
          }
      
      }
      

      }

      Thank you again I m so close to it.Thank you a lot.

      L 1 Reply Last reply
      0
      • G George Tourtsinakis

        Let's say I have a directory c:\test with some files in it.I want this directory to be copied in desktop with every file in it.If it exists I want to replace it with the new files .I m making a program so I can have backup of my projects easily and not copying pasting with windows clicks all the time . This is the whole code which creates a folder in desktop Downloads\Downloads and also some files are copies to desktop\Downloads and some others to desktop\Downloads\Downloads.

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;
        using System.IO;//We need this namespace to be used so we can have the file class
        //that gives us the ability to copy,delte files etc.

        namespace EasyCopyFiles
        {
        public partial class Form1 : Form
        {

            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        
        
            public Form1()
            {
                InitializeComponent();
        
        
        
            }
        
            private void button1\_Click(object sender, EventArgs e)
            {
        
        
                foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text))//SourceOneToCopy
                    //textbox we read from user input exammple c:\\test
                {
                    //OutputDirectory1 also a text box we read from user example d:\\test
                    File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true);
        
                    if (YesRadioButton.Checked == true)
                    {
                        desktop += "\\\\Downloads";//With the \\\\ we create the path we want
                        if (!System.IO.Directory.Exists(desktop))
                        {
        
                            DirectoryInfo directory = Directory.CreateDirectory(desktop);
        
                        }
        
                        File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true);
        
        
                    }
        
                }
            }
        
        }
        

        }

        Thank you again I m so close to it.Thank you a lot.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Yes, but what is the problem?

        G 1 Reply Last reply
        0
        • L Lost User

          Yes, but what is the problem?

          G Offline
          G Offline
          George Tourtsinakis
          wrote on last edited by
          #5

          The problem is that either I can create a new empty folder or if the folder already exists it replaces the files .If the folder doesn't exist I get an error a bug or something which crashes.

          L 1 Reply Last reply
          0
          • G George Tourtsinakis

            The problem is that either I can create a new empty folder or if the folder already exists it replaces the files .If the folder doesn't exist I get an error a bug or something which crashes.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            octapuslabs wrote:

            an error a bug or something which crashes.

            Well, since we cannot see your screen, we cannot guess which, or what any of them might say. Please provide all the information relating to your problem, including the full details of the error, and the point in your code where it occurs.

            G 1 Reply Last reply
            0
            • L Lost User

              octapuslabs wrote:

              an error a bug or something which crashes.

              Well, since we cannot see your screen, we cannot guess which, or what any of them might say. Please provide all the information relating to your problem, including the full details of the error, and the point in your code where it occurs.

              G Offline
              G Offline
              George Tourtsinakis
              wrote on last edited by
              #7

              System.IO.DirectoryNotFoundException

              {"Could not find a part of the path 'd:\\test\\130123094607-kevin-durant-dunk-lamar-odom-012313.1200x672.jpg'."}

              This happens when we have for example to copy c:\test to d:\test and the d:\ doesn't have the folder.If I create the folder manually the error disappears but the thing is I want the folder to be created if it doesn't exist and if it does exist to overwrite every file in it. Thank you again for your time.

              L 1 Reply Last reply
              0
              • G George Tourtsinakis

                System.IO.DirectoryNotFoundException

                {"Could not find a part of the path 'd:\\test\\130123094607-kevin-durant-dunk-lamar-odom-012313.1200x672.jpg'."}

                This happens when we have for example to copy c:\test to d:\test and the d:\ doesn't have the folder.If I create the folder manually the error disappears but the thing is I want the folder to be created if it doesn't exist and if it does exist to overwrite every file in it. Thank you again for your time.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                Then you need first to test if the directory exists, and if not then create it. Similarly when creating files, if they exist in the folder you need to ensure you use a create option that will force it to a new file.

                G 1 Reply Last reply
                0
                • L Lost User

                  Then you need first to test if the directory exists, and if not then create it. Similarly when creating files, if they exist in the folder you need to ensure you use a create option that will force it to a new file.

                  G Offline
                  G Offline
                  George Tourtsinakis
                  wrote on last edited by
                  #9

                  Yup I thought of that creating first the directory and then copying files but the thing I can t do both.I m having some problem probably in code which I hope I ll find it slowly.The problem is that I can t copy the files when creating the new folder only if it s already there.Thank you again sir.

                  P 1 Reply Last reply
                  0
                  • G George Tourtsinakis

                    Yup I thought of that creating first the directory and then copying files but the thing I can t do both.I m having some problem probably in code which I hope I ll find it slowly.The problem is that I can t copy the files when creating the new folder only if it s already there.Thank you again sir.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #10

                    It's a two step process. Check to see if the directory exists that you want to copy into and then do the copy. That's what you have to do if you want to stay in the .NET world (of course, you could always spin up a Process and run an xcopy to do the requisite copy).

                    G 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      It's a two step process. Check to see if the directory exists that you want to copy into and then do the copy. That's what you have to do if you want to stay in the .NET world (of course, you could always spin up a Process and run an xcopy to do the requisite copy).

                      G Offline
                      G Offline
                      George Tourtsinakis
                      wrote on last edited by
                      #11

                      Yup I m new and I know net is great.I ll find it slowly.

                      P 1 Reply Last reply
                      0
                      • G George Tourtsinakis

                        Yup I m new and I know net is great.I ll find it slowly.

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #12

                        Okay, you can take advantage of the fact that Directory.CreateDirectory only creates a directory if it doesn't exist so you could do the following:

                        private void CopyFiles(string sourceDirectory, string targetDirectory)
                        {
                        Directory.CreateDirectory(targetDirectory);
                        string[] files = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);
                        foreach (string file in files)
                        {
                        string targetFile = Path.Combine(targetDirectory, file.Replace(sourceDirectory, string.Empty));
                        File.Copy(file, targetFile);
                        }
                        }

                        I've just typed this into the editor, so you might need to fix a syntax mistake or two, but this should roughly do what you're trying to accomplish.

                        G 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Okay, you can take advantage of the fact that Directory.CreateDirectory only creates a directory if it doesn't exist so you could do the following:

                          private void CopyFiles(string sourceDirectory, string targetDirectory)
                          {
                          Directory.CreateDirectory(targetDirectory);
                          string[] files = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);
                          foreach (string file in files)
                          {
                          string targetFile = Path.Combine(targetDirectory, file.Replace(sourceDirectory, string.Empty));
                          File.Copy(file, targetFile);
                          }
                          }

                          I've just typed this into the editor, so you might need to fix a syntax mistake or two, but this should roughly do what you're trying to accomplish.

                          G Offline
                          G Offline
                          George Tourtsinakis
                          wrote on last edited by
                          #13

                          Thank you sir a lot .I ll try in next days and I ll tell you result.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups