• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3017
  • Last Modified:

Double backslashes appearing in string path C#

I have a application that was working perfectly, i have just moved my website to a different server and the save path of created xml files has changed, instead of having it hard coded, i decided to place the path in the appsettings so i could edit it in the future if the path ever changed again, without having to recompile.

my problem is that when the path is getting picked up from the web config, the backslashes are being duplicated and the application doesnt see the path as a legitimate windows path giving the following error

The given path's format is not supported.

How can i stop this from happening.

Code in webconfig

            <add key="SaveLocation" value="c:\home\c\o\complus\www\"/>

picked up in the code like so:

string savelocation = System.Configuration.ConfigurationManager.AppSettings["SaveLocation"].ToString();
1 Solution
Access the variable savelocation prefixing @.

Also check if the path is evaluating to the right location by debugging/logging it at the savelocation path assignment.
QuinnDesterAuthor Commented:
like this?

string savelocation = System.Configuration.ConfigurationManager.AppSettings[@("SaveLocation")].ToString();
No, like:
            <add key="SaveLocation" value=@"c:\home\c\o\complus\www\"/>

Open in new window

Hope it helped :)
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

QuinnDesterAuthor Commented:
Thanks... I didnt think it needed that in the webconfig xml as it isnt compiled

Ill give it a go and let you know
QuinnDesterAuthor Commented:
I have fixed it...

it wasnt the duplicated backslashes causing the problem, it still shows them but must be an issue with the debugger..

the problem was this line

string xmlpostfix = ( HttpContext.Current.Request.ServerVariables["HTTP_HOST"]);

as i was running it in debugger that was evaluating as localhost:nameofserver, this was creating an invalid file name, i hard coded a url in its place and it works perfectly

Thanks for trying.
Curtis BrooksCommented:
I too am having this problem.  Placing and "@" in the web.config does not work. It actually produces an error in the web.config file.

I am trying to retrieve local path data from the web.config file and C# is adding \ slashes to everything so that an item in the web.config that reads
    <add key="DevCromerrCorUrl" value="\\devdotnet\wwwroot\air\CROMERR_COR\"/>
is returned to the method as "\\\\devdotnet\\wwwroot\\air\\CROMERR_COR\\"

Is this some subtle way that microsoft tries to tell us not to use these sort of things in web programming? By the way we are using server 2012 R2

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now