代码如下
/// <summary>
/// 复制文件
/// </summary>
/// <param name=”sourceName”>需要复制的文件</param>
/// <param name=”folderPath”>保存到文件夹</param>
/// <param name=”fileName”>保存文件名称</param>
public void CopyToFile(string sourceName, string folderPath,string fileName)
{
//例子:
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
//目标整体路径
string targetPath = Path.Combine(folderPath, fileName);
//Copy到新文件下
FileInfo file = new FileInfo(sourceName);
if (file.Exists)
{
//true 为覆盖已存在的同名文件,false 为不覆盖
file.CopyTo(targetPath, true);
}
}