删除文件,从另一个文件夹复制同名文件 C#

Delete files, copy the same named files from another folder C#

我在 windows 表单应用程序中遇到了几个问题。到目前为止,我已经设法遍历一个文件夹并在文本框中显示结果。这些返回后,用户可以检查结果并使用以下方法删除错误文件;

private void button2_Click(object sender, EventArgs e)
{
    foreach (string file in Directory.GetFiles(@"\" + textBox1.Text + @"\d$\NSB\Coalition\EDU", "*.err").Where(item => item.EndsWith(".err")))
    {
        File.Delete(file);
    }
}

删除错误文件后,我现在想做的是从备份文件夹中复制相同的文件(文件名的唯一区别是文件扩展名)我正在使用单独的按钮执行此操作.对于这最后一步的任何帮助,我们将不胜感激。

使用路径 class 获取不带扩展名的文件名,组合路径等,例如:

StringCollection filesToBeReplaced = new StringCollection();

private void button2_Click(object sender, EventArgs e)
{
foreach (string file in Directory.GetFiles(@"\" + textBox1.Text +      @"\d$\NSB\Coalition\EDU", "*.err").Where(item => item.EndsWith(".err")))
{
  //Now you have file names without extension        
  filesToBeReplaced.Add(Path.GetFileNameWithoutExtension (file));
      File.Delete(file);
  }
}
private void CopyGoodFilesFromSource()
{
  foreach(string fileName in filesToBeReplaced)
  {
     string sourceFilePath = Path.Combine("YOUR FOLDER FOR GOOD FILES", 
Path.ChangeExtension(fileName,"Your Extension")) ;
      string destinationPath = Path.Combine("Destination Folder",
Path.ChangeExtension(fileName, "Your Extension in destination folder");

     File.Copy(sourceFilePath , destinationPath, true);
  }
}