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