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

Posted by SheoNarayan under C# category on | Views : 4213
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 = "";

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