예제 #1
0
        private void importURLsBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title       = "URLs To Scrape";
            ofd.Filter      = "Text Files|*.txt";
            ofd.Multiselect = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                URLMgr.Clear();
                uint badUrls = 0;
                foreach (string file in ofd.FileNames)
                {
                    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) //IMP: Add Exception Checks
                        using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                        {
                            string url;
                            while ((url = sr.ReadLine()) != null)
                            {
                                if (Uri.IsWellFormedUriString(url, UriKind.Absolute) && !URLMgr.Contains(url))
                                {
                                    URLMgr.Add(url);
                                }
                                else
                                {
                                    badUrls++;
                                }
                            }
                        }
                }

                //Update UI
                ScapePanelUI();

                MessageBox.Show(string.Format("A total of {0} URLs have been imported for scraping.{1}", URLMgr.Count.ToString(),
                                              badUrls > 0 ? string.Format("{0}{1} URLs seem to be invalid and were thus omitted.", Environment.NewLine, badUrls.ToString()) : ""),
                                "Import URLs", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }