0

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!

4
  • Can you tell us the text in the error? It could be an incorrect path. Commented Apr 9, 2014 at 22:10
  • possible duplicate of how to embed single backslash in string in c# Commented Apr 9, 2014 at 22:11
  • For future questions try to see if question is really scoped to what you trying to do - there is nothing special about file path - you could have asked more generic question about just strings... (but I see that it would be immediate duplicate instead of surviving for some time). Commented Apr 9, 2014 at 22:13
  • @B.K. I guess it must be an incorrect path. Commented Apr 9, 2014 at 22:17

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I see. The issue is my function breaks at the line where projectPath is used, so I assumed that was the problem.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.