Sunday, July 16, 2006

C#: How do download files from a http location to a file share

In my previous project, we had to move files from one domain to other. The files on the source domain will be uploaded to a http location and we have to move it to another location. We couldnt do a simple DTS packages here as DTS/ SSIS supports data transfer using FTP only (I guess...If you know some method, pls let me know)

You can use the below code to download a file from a http location

string remoteUri = "http://i.microsoft.com/h/en-us/i/HP_13.5/";
string fileName = "MediaCenter_S.jpg";
string myStringWebResource = null;

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri+fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);


// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, "D:\\SANGOM\\" + fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);

Remember you need to include the namespace system.net

GetTechieHere...

0 Comments:

Post a Comment

<< Home