7

I am just testing some code at the moment, although when calling the StartRemoveDuplicate (when its compiled) an Exception is thrown, complaining about illegal characters:

error

My code is as follows:

 class Program
    {
        static void Main(string[] args)
        {
            foreach (string exename in System.IO.File.ReadAllLines("test.txt"))
            {
                Process.Start("test.exe", "\"" + exename + "\"").WaitForExit();
            }

            StartRemoveDuplicate();

        }



        private static void RemoveDuplicate(string sourceFilePath, string destinationFilePath)
        {
            var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);

            File.WriteAllLines(destinationFilePath, readLines.Distinct().ToArray(), Encoding.Default);
        }


        private static void StartRemoveDuplicate()
        {
            RemoveDuplicate("C:\test.txt", "C:\test2.txt");
        }

    }
Brandon
  • 68,708
  • 30
  • 194
  • 223
Michael
  • 101
  • 1
  • 3
  • 5

4 Answers4

16

Try to use @ before the string like :

@"C:\test.txt"

or to escpe the "\" caracter

"C:\\test.txt"
Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
4

Backslash is considered a special character in C# strings, usually used to escape other characters. So you can tell it to treat backslashes as normal by prefixing your literals with @ before the quotes:

RemoveDuplicate(@"C:\test.txt", @"C:\test2.txt");

Or, you can escape it with double backslashes:

RemoveDuplicate("C:\\test.txt", "C:\\test2.txt");
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
3

the \t in C:\test is probably being seen as a tab.

MGZero
  • 5,812
  • 5
  • 29
  • 46
2

Use Path.Combine to combine parts of file paths. It handles the details of "\" characters.

kareem
  • 753
  • 1
  • 5
  • 10