C# Reading Paths From Text File Says Path Doesn't Exist

I'm developing a small command line utility to remove files from a directory. The user has the option to specify a path at the command line or have the paths being read from a text file.

Here is a sample text input:

C:\Users\MrRobot\Desktop\Delete
C:\Users\MrRobot\Desktop\Erase
C:\Users\MrRobot\Desktop\Test

My Code:

class Program
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Number of command line parameters = {0}", args.Length);

            if(args[0] == "-tpath:"){

                    clearPath(args[1]);
            }
            else
                if(args[0] == "-treadtxt:"){

                    readFromText(args[1]);
                }

        }

        public static void clearPath(string path)
        {
            if(Directory.Exists(path)){

                int directoryCount = Directory.GetDirectories(path).Length;

                if(directoryCount > 0){

                    DirectoryInfo di = new DirectoryInfo(path);

                    foreach (DirectoryInfo dir in di.GetDirectories())
                    {
                        dir.Delete(true); 
                    }

                }
                else{

                    Console.WriteLine("No Subdirectories to Remove");
                }

                int fileCount = Directory.GetFiles(path).Length;

                if(fileCount > 0){

                    System.IO.DirectoryInfo di = new DirectoryInfo(path);

                    foreach (FileInfo file in di.GetFiles())
                    {
                            file.Delete(); 
                    }


                }
                else{

                    Console.WriteLine("No Files to Remove");
                }

                }
            else{

                Console.WriteLine("Path Doesn't Exist {0}", path);
            }
        }

        public static void readFromText(string pathtotext)
        {
                try
                {   // Open the text file using a stream reader.
                    using (StreamReader sr = new StreamReader(pathtotext))
                    {
                        // Read the stream to a string, and write the string to the console.
                        string line = sr.ReadToEnd();
                        clearPath(line);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }

        }

    }
}

My Problem:

When reading from the text file, it says that the first path doesn't exist, and prints all the paths to the prompt, despite that I have no Console.WriteLine(). However, if I plug these same paths and call -tPath: it will work. My issue seems to be in the readFromText() I just can't seem to figure it out.

Jon Skeet
people
quotationmark

This is the problem:

string line = sr.ReadToEnd();

That isn't reading a line of data - it's reading the whole file in one go. Your clearPath method expects its parameter to be a single path, not a bunch of lines glued together. The reason you're seeing all the paths is that clearPath is being called (with everything in one call), that "glued together" path isn't being found, and it's printing out the input in Console.WriteLine("Path Doesn't Exist {0}", path);.

I suspect you should use something like:

var lines = File.ReadAllLines(pathtotext);
foreach (var line in lines)
{
    clearPath(pathtotext);
}

people

See more on this question at Stackoverflow