直接使用 FileInfo.CopyTo 方法
代碼如下:
public void saveFile(string filePathName , string toFilesPath)
{
FileInfo file = new FileInfo(filePathName);
string newFileName= file.Name;
file.CopyTo(toFilesPath + @"\" + newFileName, true);
}
參數說明:
filePathName:將要被復制的文件的完整路徑
toFilesPath:復制到所指定的文件夾的完整路徑
調用示例:
string filePath = @"C:\test1\hello.jpg";
string toFilesPath = @"D:\test2";
saveFile(filePath , toFilesPath )
方法重載:復制后改變文件名稱
public void saveFile(string filePathName , string toFilesPath , string newFileName)
{
FileInfo file = new FileInfo(filePathName);
file.CopyTo(toFilesPath + @"\" + newFileName, true);
}