Tutorial :illegal character in path



Question:

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.


Solution:1

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.


Solution:2

Try this:

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


Solution:3

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("\"", "");  


Solution:4

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";  


Solution:5

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? ;-)


Solution:6

try

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »