Windows Develop Bookmark and Share   
 index > Windows Forms General > Load MHT files open as stream/string in WebBrowser contol
 

Load MHT files open as stream/string in WebBrowser contol

I'm trying to load an MHT file to a WebBrowser control from a stream. If I use the Navigate method, there's no problem. However, if I use the DocumentStream property it doesn't load correctly.

Is there any way to load it directly from a stream, without saving a temp file on disk?

I tried:

WebBrowser1.DocumentText = global::TestProject.MyResourceFile.MHTFileName;

But it's print the content of the file as if I opened it in Notepad

Thanks in advance for any help given.

Death Spirit  Wednesday, September 26, 2007 2:04 PM

For the "one file" requirement, you probably want to add the mht to your executable as a resource. This can be done by viewing the project's properties on the Resources tab. Click Add Resource, Add Existing File. Choose the file. After doing this,I would recommend changing the FileType on the Properties window to Binary.

You could now use System.IO.Path.GetTempFileName andSystem.IO.File.WriteAllBytes(tempFileName,

Properties.Resources.MyResourceName)to write it to a temporary file. Then use the WebBrowser.Navigate method on that temporary file.

If you really do not want to write the temp file, I think the only alternative is to implement a "temporary protocol handler" (i.e., a special prefix such as custom:// instead of http://). This is entirely COM interop and involves the IInternetSession::RegisterNameSpace method. It requires you provide classes implementing the IInternetProtocol and IInternetProtocolRoot interfaces. This may well be too much work for your scenario.

BinaryCoder  Thursday, September 27, 2007 1:57 AM
Why is the alternate method so important?
OmegaMan  Wednesday, September 26, 2007 3:13 PM

There is a requirement: application must be in one file. Without sepparate and temporary files

Death Spirit  Wednesday, September 26, 2007 3:46 PM

For the "one file" requirement, you probably want to add the mht to your executable as a resource. This can be done by viewing the project's properties on the Resources tab. Click Add Resource, Add Existing File. Choose the file. After doing this,I would recommend changing the FileType on the Properties window to Binary.

You could now use System.IO.Path.GetTempFileName andSystem.IO.File.WriteAllBytes(tempFileName,

Properties.Resources.MyResourceName)to write it to a temporary file. Then use the WebBrowser.Navigate method on that temporary file.

If you really do not want to write the temp file, I think the only alternative is to implement a "temporary protocol handler" (i.e., a special prefix such as custom:// instead of http://). This is entirely COM interop and involves the IInternetSession::RegisterNameSpace method. It requires you provide classes implementing the IInternetProtocol and IInternetProtocolRoot interfaces. This may well be too much work for your scenario.

BinaryCoder  Thursday, September 27, 2007 1:57 AM

You can use google to search for other answers

Custom Search

More Threads

• Rich Text Box with Multithreading
• ToolMenuStrip Problem
• Visual Studio question.
• RichTextBox Removes CR from CRLF LineBreaks
• Access Javascript fns from WebBrowser C#
• How do I share variables between form controls' event-methods???
• PRINTING
• Semi-transparent background form with opaque icons
• Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
• Textbox wierd problems when a reg file is displayed!