【发布时间】:2018-04-20 06:11:51
【问题描述】:
我们如何将一个文件夹中的文件夹复制并移动到另一个文件夹。
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");
}
}
我有上面的代码来复制和移动文件夹,我没有收到任何编译错误。但是,当我尝试单击输出表单上的按钮时,它显示为终止。
更新
代码可以正常工作,但由于无法创建文件而出现终止错误,因为它已经存在
【问题讨论】:
-
showing as termination是什么意思.. 你的意思是应用程序崩溃了吗? -
如果任何文件是打开的,你就不允许这样做,它很可能会崩溃
-
可能是您没有权限。尝试以管理员身份运行。
-
@这是说 ass 无法创建文件,因为它已经存在用于移动和复制它的显示为关注文件夹名称是目录而不是文件,你需要复制新文件夹吗桌面