MHT is called "Web Single File". As the name implies, the Web page contains images, CSS files and HTML files are all placed in an MHT file. and the browser can be directly read access.
Because of the project needs, you need to save the specified Web page file as an MHT file. So I searched the Internet for relevant information. Found a copy of the code. Passed after testing.
The implementation process is now documented as follows:
Step 1: Project reference file: Install directory/system32/cdosys.dll (C:/windows/system32/cdosys.dll), which will add two namespaces: ADODB, CDO.
Step 2: Put a button button1, add the following code in its Click event:
- private void button1_click (object sender, EventArgs e)
- {
- string url = "http://www.qq.com";
- Try
- {
- Cdos. IMessage msg = new CDO. MessageClass ();
- Cdos. Configuration C = new CDO. Configuration ();
- Msg. Configuration = C;
- //First parameter is URL, second parameter is support format, third parameter is User ID, fourth parameter is user password
- Msg. Createmhtmlbody (URL, CDO. Cdomhtmlflags.cdosuppressall, " ", "" ");
- Msg. GetStream (). SaveToFile (@"d:/1.mht", ADODB. Saveoptionsenum.adsavecreateoverwrite);
- MessageBox.Show ("Save OK!!!");
- }
- catch (Exception ex)
- {
- MessageBox.Show ("Error:" + ex.) Message);
- }
- }
C # code implementation to save a Web page file as an MHT file