C # - Unhandled exception - Illegal characters in transit

I am just testing the code at the moment, although an exception is thrown when calling StartRemoveDuplicate (when it is compiled), complaining about invalid 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"); } } 
+7
source share
4 answers

Try using @ before a line like:

 @"C:\test.txt" 

or to save the "\" caracter

 "C:\\test.txt" 
+15
source

A backslash is considered a special character in C # strings, usually used to exit other characters. So you can say that it treats the backslash as usual, the prefix of your @ literals before the quotes:

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

Or you can avoid it with a double backslash:

 RemoveDuplicate("C:\\test.txt", "C:\\test2.txt"); 
+4
source

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

+3
source

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

+2
source

All Articles