/// <summary> /// 拷贝文件到另一个文件夹下 /// </summary> /// <param name="sourceName">新文件路径</param> /// <param name="folderPath">需替换的文件夹路径1</param> ///<param name="str3">需替换的文件夹路径2</param> public void CopyToFile(string sourceName, string folderPath, string str3) { try { if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //当前文件如果不用新的文件名,那么就用原文件文件名 string fileName = Path.GetFileName(sourceName); //这里可以给文件换个新名字,如下: //string fileName = string.Format("{0}.{1}", "newFileText", "txt"); //目标整体路径 string targetPath = Path.Combine(folderPath, fileName); string targetPath2 = Path.Combine(str3, fileName); //Copy到新文件下 FileInfo file = new FileInfo(sourceName); if (file.Exists) { //true 为覆盖已存在的同名文件,false 为不覆盖 file.CopyTo(targetPath, true); file.CopyTo(targetPath2, true); } } catch (Exception ex) { MessageBox.Show("异常错误" + ex.Message); } }