【问题标题】:How to Copy and move files and folders in C# [duplicate]如何在 C# 中复制和移动文件和文件夹 [重复]
【发布时间】: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 无法创建文件,因为它已经存在用于移动和复制它的显示为关注文件夹名称是目录而不是文件,你需要复制新文件夹吗桌面

标签: c# winforms


【解决方案1】:
【解决方案2】:

我不确定您要做什么,但我发现这里有很多问题。

1) 在行中

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

您在第二个参数中使用// 作为目录分隔符。你应该把它改成

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

2) 有时您不正确地使用逐字字符串。例如,在行中

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

您正在使用逐字字符串,这意味着编译器会忽略该字符串中的转义序列。因此,您的应用程序稍后将找不到该路径。相反,请使用以下方法之一:

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但前提是后者尚不存在。如果它已经存在,则该行将导致异常(这可能会使您的应用程序终止)。

此外,在您已经移动了上面显示的行中的目录内容之后,您再次尝试移动该行中的某些内容

File.Move(path, path2);

但是pathpath2 是描述目录而不是文件的字符串,所以我不会那样做。此外,由于您已经在上一行中移动了目录(更准确地说:它的内容),所以我问自己该行的确切目的是什么。

我还没有查看您的 BtncopyClick,所以现在让我们专注于 BtnmoveClick。请尝试修复目前描述的问题,如果您还有其他问题,请反馈。

作为一般建议:如果你真的想学习 C#,那么不要复制和粘贴随机选择的示例;你永远不会以这种方式学到任何有用的东西。相反,请阅读 MSDN 上的 .net 框架文档或阅读一本好书——这将使您有更深入的了解。

【讨论】:

  • 我已采纳您的建议,感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2014-08-15
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
相关资源
最近更新 更多