C# 如何在C中复制和移动文件和文件夹#
如何将一个文件夹中的文件夹复制并移动到另一个文件夹C# 如何在C中复制和移动文件和文件夹#,c#,winforms,C#,Winforms,如何将一个文件夹中的文件夹复制并移动到另一个文件夹 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=@"
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");
}
}
我有上面的代码来复制和移动文件夹,我没有得到任何编译错误。然而,当我试图点击输出表单上的按钮时,它显示为终止
更新
代码不会出现任何错误,但由于无法创建文件而导致终止错误,因为它已经存在我不确定您试图执行的操作,但我在这里看到了很多问题 1) 排队
System.IO.Directory.Move("E:\\Files\\25-11-2017",@"E://Anusha//aaa");
string sourcepath=@"E:\\Anusha";
File.Move(path, path2);
您在第二个参数中使用了/
作为目录分隔符。你应该把它改成
System.IO.Directory.Move("E:\\Files\\25-11-2017","E:\\Anusha\\aaa");
2) 有时您不正确地使用逐字字符串。例如,在队列中
System.IO.Directory.Move("E:\\Files\\25-11-2017",@"E://Anusha//aaa");
string sourcepath=@"E:\\Anusha";
File.Move(path, path2);
您使用的是逐字字符串,这意味着编译器忽略该字符串中的转义序列。因此,以后的应用程序将找不到该路径。相反,请使用以下选项之一:
string sourcepath=@"E:\Anusha";
string sourcepath="E:\\Anusha";
3) BtnmoveClick的结构非常奇怪。线路
System.IO.Directory.Move("E:\\Files\\25-11-2017","E:\\Anusha\\aaa");
将E:\files\25-11-2017
的内容移动到E:\Anusha\aaa
,,但前提是后者尚不存在。如果它已经存在,该行将导致异常(这可能会导致应用程序终止)
此外,在您已经移动了上面显示的行中的目录内容之后,您再次尝试移动行中的内容
System.IO.Directory.Move("E:\\Files\\25-11-2017",@"E://Anusha//aaa");
string sourcepath=@"E:\\Anusha";
File.Move(path, path2);
但是path
和path2
是描述目录而不是文件的字符串,所以我不会这样做。此外,由于您已经在前一行中移动了目录(更准确地说:它的内容),因此我要问自己这一行的确切目的是什么
我还没有查看你的BtncopyClick,所以现在让我们集中精力研究BtnmoveClick。请尝试修复到目前为止描述的问题,如果您还有其他问题,请向我们报告
作为一般建议:如果你真的想学习C#,那么不要复制和粘贴随机选择的示例;那样你永远学不到有用的东西。相反,请阅读MSDN上的.net framework文档或阅读一本好书-这将使您深入理解。希望这对您有所帮助
您可以使用命名空间中的、和类。显示为终止的
是什么意思。。你的意思是应用程序崩溃了吗?如果有任何文件是打开的,你不允许这样做,那么它很可能会崩溃,因为你没有权限。请尝试以管理员身份运行。@它的意思是ass无法创建文件,因为它已存在用于移动和复制的文件。显示为“关注”文件夹名称是目录而不是文件,是否需要在桌面上复制新文件夹?我已采纳了您的建议,谢谢您的帮助。