anna
anna

Reputation: 1011

escape quotes and @ in web.configs

Hi i'm trying to get the following back from the web.config

@"Chief\adama";

and will use it as follows:

var queuePath = @"Chief\adama";

at the moment i have:

 <add key="AdamaPath" value="@Chief\adama" />

it is actually only the Chief part which will change and i have tried the following:

web.config:

<add key="AdamaPath" value="Chief" />

.cs:

var machine = ConfigurationManager.AppSettings["AdamaPath"];
var queuePath = "@" + "\"" + machine + "\adama" + "\"";

but i get: "@ \"Chief\adama\""

I have also tried:

 <add key="AdamaPath" value="@ &quot; Chief\adama &quot;" />

which also gets:

"@ \" Chief\adama \""

can anyone let me know how to get rid of the extra " and \ thanks!

Upvotes: 1

Views: 844

Answers (2)

Oded
Oded

Reputation: 499262

The following is probably all you need:

<add key="AdamaPath" value="Chief" />

var machine = ConfigurationManager.AppSettings["AdamaPath"];
var queuePath = machine + "\\adama";

The above will produce a string containing Chief\adama, which seems to be what you need.

The @ signifies a verbatim string literal (when preceding a string literal) - you are constructing strings that contain this, which can't work as it is a C# features and needs to precede string literals.

Upvotes: 2

James
James

Reputation: 82136

I think you are possibly confusing escape characters for actual string characters. If you just want to pull the value from the web.config do:

var machine = ConfigurationMananger.AppSettings["AdamaPath"];
var queuePath = String.Format("{0}{1}adama", machine, Path.DirectorySeparatorChar);

Upvotes: 2

Related Questions