![](/img/trans.png)
[英]Moving a folder (Directory) from one location to another - misbehavior
[英]Moving folder from one location to another in a web service
我有一個Web服務,用於查找文件夾(C:\\ Incoming)中的文件,並通過電子郵件將它們發送到指定的電子郵件地址。 我希望能夠移動該文件夾,一旦它被郵寄到另一個文件夾(C:\\ Processed)。
我嘗試使用下面的代碼,但它不起作用。
string SourceFile = "C:\\Incoming\\" + "" + Year + "" + Month + "" + Day + "";
string destinationFile = "C:\\Processed" + "" + Year + "" + Month + "" + Day + "";
System.IO.File.Move(SourceFile , destinationFile);
我收到一條錯誤消息,指出無法找到源文件。 我已經確認它確實存在,我可以訪問它。
您正在移動文件夾而不是文件,您需要迭代文件以逐個復制。
string Source = "C:\\Incoming\\" + "" + Year + "" + Month + "" + Day + "";
string destination = "C:\\Processed" + "" + Year + "" + Month + "" + Day + "";
DirectoryInfo di = new DirectoryInfo(Source);
FileInfo[] fileList = di.GetFiles(".*.");
int count = 0;
foreach (FileInfo fi in fileList)
{
System.IO.File.Move(Source+"\\"+fi.Name , destinationFile+"\\"+fi.Name);
}
使用String.Format
進行一次,使用System.IO.File.Exists()
來確保文件存在。
string SourceFile = String.Format("C:\\Incoming\\{0}{1}{2}",Year,Month,Day);
string destinationFile = String.Format("C:\\Processed\\{0}{1}{2}",Year,Month,Day);
if (System.IO.File.Exists(SourceFile) {
System.IO.File.Move(SourceFile , destinationFile);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.