[英]Copy file to directory
是否沒有用於將文件復制到目錄的.NET庫調用? 我發現的所有庫調用(例如File.Copy()
或FileInfo.CopyTo()
)僅支持將文件復制到另一個完全指定的文件 。
string file = "C:\Dir\ect\ory\file.txt";
string dir = "C:\Other\Directory";
File.Copy(file, dir); // does not work, requires filename
有圖書館電話嗎? 如果不是,編寫自己的實用程序的最佳方法是什么,真的需要使用Path.GetFileName()
嗎?
我真的必須使用Path.GetFileName()嗎?
究竟:
string destination = Path.Combine(dir, Path.GetFileName(file));
Directory.CreateDirectory(dir);
File.Copy(file, destination);
試試這個例子
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.