2012-10-18 35 views
0

我有一個Web服務,用於在文件夾(C:\ Incoming)中查找文件並將它們通過電子郵件發送到指定的電子郵件地址。我希望能夠移動該文件夾,一旦它被郵寄到另一個文件夾(C:\ Processed)。將文件夾從Web服務中的一個位置移動到另一個位置

我嘗試使用下面的代碼,但它不起作用。

string SourceFile = "C:\\Incoming\\" + "" + Year + "" + Month + "" + Day + ""; 
string destinationFile = "C:\\Processed" + "" + Year + "" + Month + "" + Day + ""; 
System.IO.File.Move(SourceFile , destinationFile); 

我得到一個錯誤,說無法找到源文件。我已經證實它確實存在,我可以訪問它。

+0

末添加文件擴展名 – Zaki

回答

2

您正在移動文件夾而不是文件,您需要遍歷文件以逐個複製。

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); 
} 
0

使用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); 
} 
相關問題