[英]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);
}
}
在删除错误文件之后,现在要做的是从备份文件夹复制相同的文件(文件名的唯一区别是文件扩展名),我正在使用单独的按钮执行此操作。 最后一步的任何帮助将不胜感激。
例如,使用Path类获取不带扩展名的文件名,合并路径等,例如:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.