0

How can we copy and move folders in one folder to another folder.

void BtncopyClick(object sender, EventArgs e)
{
   string filename=@"E:\\Files\\Reports\\R^ECG^_0_1688^Jones^^_20160711065157_20160711065303 - Copy (4) - Copy.pdf";
   string sourcepath=@"E:\\Anusha";
   string targetpath=@"E:\\Anusha\\aaa";

   string sourcefile= System.IO.Path.Combine(sourcepath,filename);
   string destfile= System.IO.Path.Combine(targetpath,filename);
   if (!System.IO.Directory.Exists(targetpath))
   {
      System.IO.Directory.CreateDirectory(targetpath);
   }
   System.IO.File.Copy(sourcefile, destfile, true);
   if (System.IO.Directory.Exists(sourcepath))
   {
      string[] files = System.IO.Directory.GetFiles(sourcepath);

      // Copy the files and overwrite destination files if they already exist.
      foreach (string s in files)
      {
         // Use static Path methods to extract only the file name from the path.
         filename = System.IO.Path.GetFileName(s);
         destfile = System.IO.Path.Combine(targetpath, filename);
         System.IO.File.Copy(s, destfile, true);
      }
   }
   else
   {
      MessageBox.Show("File doesn't exist");
   }
}
void BtnmoveClick(object sender, EventArgs e)
{
   String path = "E:\\Files\\25-11-2017";          
   String path2 = "E:\\Anusha\\aaa\\25-11-2017";

   if (!File.Exists(path)) 
   {
      {
         // This statement ensures that the file is created, 
         // but the handle is not kept. 
         using (FileStream fs = File.Create(path)) {}
      }

      System.IO.Directory.Move("E:\\Files\\25-11-2017",@"E://Anusha//aaa");

      // Move the file.
      File.Move(path, path2); 
      MessageBox.Show("File Moved");
   }
}

I have the above code to copy and move the folder,I am not getting any compiling errors. However, when i am trying to click on button on the output form it was showing as termination.

Update

Code works with out any error but it was getting termination Error as cannot create a file as it is already exists

TheGeneral
  • 79,002
  • 9
  • 103
  • 141

2 Answers2

1

Hope this helps

How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

You can use System.IO.File, System.IO.Directory, System.IO.FileInfo, and System.IO.DirectoryInfo classes from the System.IO namespace.

Charlotte
  • 11
  • 4
0

I am not sure what you are trying to do, but I am seeing a lot of problems here.

1) In the line

System.IO.Directory.Move("E:\\Files\\25-11-2017",@"E://Anusha//aaa");

you are using // as directory separator in the second argument. You should change it to

System.IO.Directory.Move("E:\\Files\\25-11-2017","E:\\Anusha\\aaa");

2) Sometimes you are using verbatim strings incorrectly. For example, in the line

string sourcepath=@"E:\\Anusha";

you are using a verbatim string which means that the compiler ignores the escape sequences in that string. Hence, your application later won't find that path. Instead, use one of the following:

string sourcepath=@"E:\Anusha";
string sourcepath="E:\\Anusha";

3) The structure of your BtnmoveClick is quite weird. The line

System.IO.Directory.Move("E:\\Files\\25-11-2017","E:\\Anusha\\aaa");

moves the contents of E:\files\25-11-2017 to E:\Anusha\aaa, but only if the latter does NOT exist yet. If it already exists, that line will cause an exception (which probably makes your application terminate).

Furthermore, after you already have moved the directory contents in the line shown above, you are again trying to move something in the line

File.Move(path, path2);

But path and path2 are strings which describe directories, not files, so I wouldn't do it that way. Furthermore, since you already have moved the directory (more precise: its contents) in the previous line, I am asking myself what exactly the purpose of that line is.

I didn't look into your BtncopyClick yet, so let's concentrate on BtnmoveClick for now. Please try to fix the issues described so far, and if you have further issues, report back.

As a general recommendation: If you really want to learn C#, then don't copy-and-paste randomly selected examples; you'll never learn anything useful that way. Instead, read the documentation of the .net framework on MSDN or read a good book - this will gain you a deep understanding.

Binarus
  • 4,005
  • 3
  • 25
  • 41