將目錄底下檔案移至別的目錄
程式如下
string dateStr = DateTime.Now.ToString("yyyyMMdd");
string sourcePath = @"C:\TEST\測試";
string destPath = @"C:\TEST\測試2\" + dateStr;
if (Directory.Exists(sourcePath))
{
//獲取目錄下所有檔案
List<string> files = new List<string>(Directory.GetFiles(sourcePath));
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
foreach (string file in files)
{
string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(file) });
//移動檔案
File.Move(file, destFile);
}
}