How to get the contents of the webpage and save into a file?

SheoNarayan
Posted by SheoNarayan under C# category on | Views : 5328
Lets say you have a requirement where you need to get the contents of a webpage and save it into a local file (file on the server), you can use following code.

            string contentToGetFrom = "http://www.dotnetfunda.com/";

using (WebClient webClient = new WebClient())
{
string localFileName = Server.MapPath("~/dotnetfunda.txt");
webClient.DownloadFile(contentToGetFrom, localFileName); // download the web page contents and save into local file
}


Above code will download the file and save its content into dotnetfunda.txt on the root of the server (provided write permission is there).

Comments or Responses

Login to post response