using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.NetworkInformation; namespace DetectAndDownload { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int successCount = 0; try { string url = textBox1.Text.Trim(); if (!url.ToLower().StartsWith("http")) { url = "http://" + url; } Uri siteUri = new Uri(url); if (siteUri.IsWellFormedOriginalString()) { ShowMessage("Geting Page: " + url); string html = HTMLutility.GetInstance.GetHTML(siteUri); ShowMessage("Got Page content. Start analyze"); //string html = "<a href="http://localhost:1311/1.gif" mce_href="http://localhost:1311/1.gif">abc</a>"; //reglar to get urls for download List<string> resources = HTMLutility.GetInstance.GetAllUrls(html); ShowMessage("There are " + resources.Count + " links."); if (resources.Count > 0) { //string[] filters = textBox2.Text.Trim().Split(','); //Dictionary<string, string> dicFilters = new Dictionary<string, string>(); //foreach (string f in filters) //{ // dicFilters.Add(f, f); //} foreach (string res in resources) { string newRes = res; if (res.StartsWith("/")) { newRes = siteUri.Host + res; } //download ShowMessage("Downloading " + newRes); string message = string.Empty; //if (newRes.Contains("/")) //{ // string tempres = newRes.Substring(HTMLutility.GetInstance.ReverseIndexOf(newRes, '/')); // if (tempres.Contains(".")) // message = tempres; //} if (newRes.StartsWith(".")) { int removeLen = siteUri.Segments[siteUri.Segments.Length - 1].Length; newRes = siteUri.AbsoluteUri.Substring(0, siteUri.AbsoluteUri.Length - removeLen) + newRes; } if (HTMLutility.GetInstance.DownLoad(newRes, ref message)) { ShowMessage("== Saved as " + message + " ==="); successCount++; } else { ShowMessage("Cancel. " + message); } } } } else { ShowMessage(url + " is not exist."); } } catch (System.Net.WebException webException) { ShowMessage(webException.Message); } finally { ShowMessage((successCount > 0 ? "Total " + successCount.ToString() + " downloaded. " : "End. ") + "==========================================="); } } private void ShowMessage(string msg) { textBox3.AppendText(System.DateTime.Now.ToShortTimeString() + " " + msg + "/r/n"); } } }