예제 #1
0
        private void ImportURLsBtn_Click(object sender, EventArgs e)
        {
            var 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 (var file in ofd.FileNames)
                {
                    using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))     //IMP: Add Exception Checks
                        using (var 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.Concat($"A total of {URLMgr.Count} URLs have been imported for scraping.",
                                              badUrls > 0 ? $"\n{badUrls} URLs seem to be invalid and were thus omitted." : string.Empty),
                                "Import URLs", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }