[c#] illegal character in path

I am trying to get to a file located in

C:\Program Files (x86)\test software\myapp\demo.exe

In VS debugger i see the path as:

"\"C:\\\Program Files (x86)\\\test software\\\myapp\\\demo.exe\""

when i print it out i see in console :

"C:\Program Files (x86)\test software\myapp\demo.exe"

but when i try something like

FileInfo fi = new FileInfo(PathMentionedAbove); 

i get Illegal character in path.

What is wrong? the file exists and path is correct. what's illegal above this path?

any help would be appreciated.

This question is related to c#

The answer is


The string is surrounded by double quotes. Yes, that's not a valid character in a path.

You should probably tackle it at the source, but you can strip them out with:

        path = path.Replace("\"", "");

You seem to have the quote marks (") embedded in your string at the start and the end. These are not needed and are illegal characters in a path. How are you initializing the string with the path?

This can be seen from the debugger visualizer, as the string starts with "\" and ends with \"", it shows that the quotes are part of the string, when they shouldn't be.

You can do two thing - a regular escaped string (using \) or a verbatim string literal (that starts with a @):

  string str = "C:\\Program Files (x86)\\test software\\myapp\\demo.exe";

Or:

  string verbatim = @"C:\Program Files (x86)\test software\myapp\demo.exe";

Try this:

string path = @"C:\Program Files (x86)\test software\myapp\demo.exe";

I usualy would enter the path like this ....

FileInfo fi = new FileInfo(@"C:\Program Files (x86)\test software\myapp\demo.exe"); 

Did you register the @ at the beginning of the string? ;-)


try

"C:/Program Files (x86)/test software/myapp/demo.exe"


Your path includes " at the beginning and at the end. Drop the quotes, and it'll be ok.

The \" at the beginning and end of what you see in VS Debugger is what tells us that the quotes are literally in the string.