0

I used this code to open the HTML page in HTML editor in c#.

private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = 
false, ValidateNames = true, Filter = "HTML|*.html" })


            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                webBrowser1.DocumentStream = fs;

            }
    }

and also I used this code to save the changes

 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog svf = new SaveFileDialog();
        svf.Filter = "Text Files (.html)|*.html";

        if (svf.ShowDialog() == DialogResult.OK)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(svf.FileName);

            sw.WriteLine(webBrowser1);
            sw.Close();
        }
    }

However, the only line is saved in my HTML page is this message: System.Windows.Forms.WebBrowser. Do you have any idea that how can I save the content of the HTML page? Thanks

1
  • This line sw.WriteLine(webBrowser1) is implicitly calling webBrowser1's ToString() method which gives you the content you're currently getting. You have to write webBrowser1's documentStream. Commented Dec 14, 2017 at 14:54

1 Answer 1

1

The following should work as you require:

System.IO.StreamWriter sw = new System.IO.StreamWriter(svf.FileName); webBrowser1.DocumentStream.CopyTo(sw.BaseStream); sw.Flush(); sw.Close();

The reason yours does not work is because you're trying to write an object to a stream directly, which as stated implicitly calls the ToString() method.

Sign up to request clarification or add additional context in comments.

1 Comment

CopyTo(sw): for the 'sw' inside the parenthesis it gave me this error: Argument1: Conot convert from 'system.IO.StreamWriter TO System.IO.stream

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.