Thursday, January 13, 2011

How to save web page as local file by C#

WebClient
Namespace: System.Net

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest.RegisterPrefix method.

Code:

protected void saveDataPsm(string fn, string url, string path2)
{
string sFileName = "c:\\Files\\test.txt";
//string sFileName = "c:\\Files\\test.txt";
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData(url);

using (FileStream fsNew = new FileStream(fn, FileMode.Create, FileAccess.Write))
{
fsNew.Write(myDataBuffer, 0, myDataBuffer.Length);
}
}


Thanks the post

1 comment:

  1. Another C# Code Format link:
    http://www.manoli.net/csharpformat/
    Later, I think I will have my owner.

    ReplyDelete