7

I have a helper class pulling a string from an XML file. That string is a file path (so it has backslashes in it). I need to use that string as it is... How can I use it like I would with the literal command?

Instead of this:

string filePath = @"C:\somepath\file.txt";

I want to do this:

string filePath = @helper.getFilePath(); //getFilePath returns a string

This isn't how I am actually using it; it is just to make what I mean a little clearer. Is there some sort of .ToLiteral() or something?

6 Answers 6

14

I don't think you have to worry about it if you already have the value. The @ operator is for when you're specifying the string (like in your first code snippet).

What are you attempting to do with the path string that isn't working?

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

3 Comments

wow... too long at the computer today... the xml file path was just wrong. There is absolutely no reason I would need to do what I was trying to do, thanks for setting me straight... I need to go to bed!
Yeah the @ only escapes values you can't legally type into a string constant. They don't need escaping when they are already in a variable as you dont have two characters for \n in your string you have one new line char.
I was facing same issue but in last I found my connection string was wrong. Your answer made me to check the connection string. thanks.
4

I'm not sure if I understand. In your example: if helper.getFilePath() returns "c:\somepath\file.txt", there will be no problem, since the @ is only needed if you are explicitely specifying a string with "".

When Functions talk to each other, you will always get the literal path. If the XML contains c:\somepath\file.txt and your function returns c:\somepath\file.txt, then string filePath will also contain c:\somepath\file.txt as a valid path.

Comments

3

The @"" just makes it easier to write string literals.

string (C# Reference, MSDN)

Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:

@"good morning" // a string literal

The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

One place where I've used it is in a regex pattern:

string pattern = @"\b[DdFf][0-9]+\b";

If you have a string in a variable, you do not need to make a "literal" out of it, since if it is well formed, it already has the correct contents.

Comments

2

In C# the @ symbol combined with doubles quotes allows you to write escaped strings. E.g.

print(@"c:\mydir\dont\have\to\escape\backslashes\etc");

If you dont use it then you need to use the escape character in your strings.

http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx

You dont need to specify it anywhere else in code. In fact doing so should cause a compiler error.

Comments

1

You've got it backwards. The @-operator is for turning literals into strings, while keeping all funky characters. Your path is already a string - you don't need to do anything at all to it. Just lose the @.

string filePath = helper.getFilePath();

Comments

1

The string returned from your helper class is not a literal string so you don't need to use the '@' character to remove the behaviour of the backslashes.

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.