How to use & in a file path

mash

New member
Joined
Nov 2, 2017
Messages
1
Programming Experience
Beginner
Hi all,
I am new to C# and am struggling with what might be a simple question.
Someone has created a Folder called "C:\Tom & Gerry" and I need to get to this.
When I put Filepath="C:\Tom& Gerry" in my program, C# is saying Invalid characters in Path.

Q: How can I get round this ?

Regards

Mash
 
Filepath="C:\Tom& Gerry"
& is a valid character in paths (check Path.GetInvalidPathChars), but if that is representing actual code you get this compiler error:
Error CS1009 Unrecognized escape sequence
This is because \ is an escape character in C# strings, and you have to escape it with another \ or use a verbatim string where you put @ before opening quote. See string (C# Reference) | Microsoft Docs
 

Latest posts

Back
Top Bottom