The easiest way is to add WebBrowser Control to the application and specify it on the page you want to save using the Navigate() method.
Then, when the document has loaded, call the ShowSaveAsDialog method . Then the user can save the page as a single file or a file with images in a subdirectory.
[Update]
Now, having noticed “programmatically” in your question, the above approach is not ideal, as it requires user participation or understanding of the Windows API to send input using SendKeys or similar.
There is nothing built into the .NET Framework that does everything you ask.
So my revised approach would be as follows:
- Use
System.NET.HttpWebRequest to get the main HTML document as a string or stream (easy). - Download this into an HTMLAgilityPack document, where now you can easily request a document to get lists of all image elements, style links, etc.
- Then create a separate web request for each of these files and save them in a subdirectory.
- Finally, update all relevant links on the home page to point to items in a subdirectory.
In essence, you will implement a very simple web browser. You may encounter problems with pages using JavaScript to dynamically change or request the contents of a page, but for most pages this should give acceptable results.
source share