File lines not being replaced, even though I coded it to do so?

I'm trying to run through each line in a file which is a link. I then add to a string list all the dead links (links that return 404 or have a specific phrase printed on the page. It all works okay so far, but the only problem I currently face is it doesn't remove them from the file as I request near the end.

Why is this?

using System.Collections.Generic;

namespace dead_link_finder
{
    using System;
    using System.IO;
    using System.Net;
    using System.Threading;

    static class Program
    {
        private static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.White;

            var fileToScan = Console.ReadLine();
            var reader = new StreamReader(fileToScan);
            string line;
            var deadLinks = new List<string>();

            while ((line = reader.ReadLine()) != null)
            {
                Log("Scanning: " + line, ConsoleColor.White);

                using (var webClient = new WebClient())
                {
                    try
                    {
                        var content = webClient.DownloadString(line);

                        if (content.Contains("text-danger"))
                        {
                            deadLinks.Add(line);
                        }
                    }
                    catch (WebException wex)
                    {
                        if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
                        {
                            deadLinks.Add(line);
                        }
                    }
                }
            }

            reader.Close();

            Console.WriteLine();
            Console.WriteLine("Found: " + deadLinks.Count + " dead links in the collection.");
            Console.WriteLine();

            Thread.Sleep(5000);

            Console.WriteLine("Removing the dead links, please wait...");

            foreach (var deadLink in deadLinks)
            {
                var str = File.ReadAllText(fileToScan);
                File.WriteAllText(fileToScan, str.Replace(deadLink, ""));
            }

            Console.WriteLine();
            Console.WriteLine("Finished...");

            Console.ReadKey(true);
        }
    }
}

1 answer

  • answered 2018-04-14 19:23 torsan

    You can try this. Parse the file contents into a list, save the dead links in another and then use the LINQ extension method Except() to get the difference.

    This code is a bit messy and tries to do alot of things in one go. You should probably split it up into more manageable pieces like ReadFile(), CheckLinks() and WriteFile() or something.

    using System.Collections.Generic;
    using System;
    using System.IO;
    using System.Net;
    using System.Threading;
    using System.Linq;
    
    namespace dead_link_finder
    {
        static class Program
        {
            private static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.White;
    
                var fileToScan = Console.ReadLine();
                var links = File.ReadAllLines(fileToScan);
                var deadLinks = new List<string>();
                var webClient = new WebClient();
    
                foreach (var link in links)
                {
                    try
                    {
                        var content = webClient.DownloadString(link);
    
                        if (content.Contains("text-danger"))
                        {
                            deadLinks.Add(link);
                        }
                    }
                    catch (WebException wex)
                    {
                        if (wex.Status == WebExceptionStatus.NameResolutionFailure )
                        {
                            deadLinks.Add(link);
                        }
                    }
                }
    
                Console.WriteLine();
                Console.WriteLine("Found: " + deadLinks.Count + " dead links in the collection.");
                Console.WriteLine();
    
                Thread.Sleep(5000);
    
                Console.WriteLine("Removing the dead links, please wait...");
    
                var justTheGoodLinks = links.Except(deadLinks);
                File.WriteAllLines(fileToScan, justTheGoodLinks);
    
                Console.WriteLine();
                Console.WriteLine("Finished...");
    
                Console.ReadKey(true);
            }
        }
    }