user1668123
user1668123

Reputation: 105

Moving folder from one location to another in a web service

I have a web service that locates files in a folder (C:\Incoming) and emails them to a specified email address. I want to be able to move that folder, once it has been mailed to another folder (C:\Processed).

I tried using this code below, but it does not work.

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

I get an error saying that the sourcefile could not be found. I have verified that it does exist and I have access to it.

Upvotes: 0

Views: 421

Answers (2)

sharafjaffri
sharafjaffri

Reputation: 2214

You are moving folders not file you will need to iterate over files to copy one by one.

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

Upvotes: 2

Eonasdan
Eonasdan

Reputation: 7765

Use String.Format for one, second use System.IO.File.Exists() to make sure the file is there.

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

Upvotes: 0

Related Questions