PTRFRLL
PTRFRLL

Reputation: 27

.NET String Literal File Path Error

I have a filepath string defined like so:

string projectPath = @"G:\filepath\example\example\";

however when I debug:

projectPath = G:\\filepath\\example\\example\\

am I missing something? I thought the @ was for literal string's so I don't have to escape backslashes. If I try escaping

string projectPath = "G:\\filepath\\example\\example\\";

I get the same problem, any ideas?

EDIT: Apparently this is a non-issue, my code just happens to break where projectPath is used:

string [] folders = Directory.GetDirectories(projectPath);

I guess I have an incorrect path?

EDIT 2: the issue wasn't the string, it was an access denied on the folder I tried to access. Oops!

Upvotes: 0

Views: 1904

Answers (2)

B.K.
B.K.

Reputation: 10152

Based on your edits and comments, I gather that you might benefit from using try{}catch(){} statements in your code. If it's an invalid IO operation, such an invalid path, you would be able to see it from the exception's message and avoid "crashes."

Example:

string projectPath = @"G:\filepath\example\example\";
string[] folders = null;

try
{
    folders = Directory.GetDirectories(projectPath);
}
catch(Exception e)
{
    Debug.WriteLine(e.Message);
}

You may also try Directory.Exists() method:

string projectPath = @"G:\filepath\example\example\";
string[] folders = null;

if (Directory.Exists(projectPath))
{
    folders = Directory.GetDirectories(projectPath);
}

Although, I prefer try{}catch(){}, just in case there's a permissions issue.

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101681

No, the debugger shows the extra backslashes but they are not actually there.So you don't need to be worry.

Upvotes: 3

Related Questions