Deena
Deena

Reputation: 41

How to save string content to a local file

I am developing an application which is showing web pages through a web browser control.

When I click the save button, the web page with images should be stored in local storage. It should be save in .html format.

I have the following code:

WebRequest request = WebRequest.Create(txtURL.Text);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;

using (StreamReader sr = new StreamReader(data))
{
    html = sr.ReadToEnd();
}

Now string html contains the webpage content. I need to save this into D:\Cache\

How do i save the html contents to disk?

Upvotes: 4

Views: 8968

Answers (3)

JimiLoe
JimiLoe

Reputation: 978

You can use this code to write your HTML string to a file:

var path= @"D:\Cache\myfile.html";
File.WriteAllText(path, html);

Further refinement: Extract the filename from your (textual) URL.

Update: See Get file name from URI string in C# for details. The idea is:

var uri = new Uri(txtUrl.Text);

var filename = uri.IsFile 
  ? System.IO.Path.GetFileName(uri.LocalPath)
  : "unknown-file.html";

Upvotes: 5

Nazmul
Nazmul

Reputation: 595

There is nothing built-in to the .NET Framework as far I know.

So my approach would be like below:

  • Use System.NET.HttpWebRequest to get the main HTML document as a string or stream (easy). (Which you have done already)

  • Load this into a HTMLAgilityPack document where you can now easily query the document to get lists of all image elements, stylesheet links, etc.

  • Then make a separate web request for each of these files and save them to a subdirectory.

  • Finally update all relevent links in the main page to point to the items in the subdirectory.

Upvotes: 0

Hemant
Hemant

Reputation: 1018

you have to write below code on save button

File.WriteAllText(path, browser.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(browser.Document.Encoding));

Now the 'Body.parent' must save whole the page instead of just saving only part.

check it.

Upvotes: 0

Related Questions