8

I'm trying to open the web configuration so I can encrypt/decrypt it but it's failing. I'm doing this on Web Developer 2010 Express on my desktop where I setup a website as a filesystem. Here are the ways I've tried.

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");

Configuration config = WebConfigurationManager.OpenWebConfiguration("/web.config");

Configuration config = WebConfigurationManager.OpenWebConfiguration(/AppNameHere);

Every time I get error: An error occurred loading a configuration file: Failed to map the path '/'.

Any ideas?

3
  • Are you trying to access an element in <appSettings> or <applicationSettings>? Commented Dec 18, 2012 at 15:47
  • Actually I'm trying to access connectionStrings section and encrypt it. Maybe I can't do that in the same program that's using the config file. Commented Dec 18, 2012 at 15:59
  • Have you tried WebConfigurationManager.ConnectionStrings? Commented Dec 18, 2012 at 16:02

2 Answers 2

16

If you are trying to open the web.config from within the application, you don't need to open it, just use:

var section = WebConfigurationManager.GetSection("mySection");

Just for you to know, the right parameter is:

Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");

See: http://msdn.microsoft.com/es-es/library/system.web.configuration.webconfigurationmanager(v=vs.100).aspx

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

Comments

9

Run your Visual Studio as Administrator. Cheers! :D

2 Comments

Have the same problem but when I tried to run my visual studio as administrator, it works like a miracle. :)
"An error occurred loading a configuration file: Failed to map the path '/'." its mean there is a permission error. and visual studio can not access the "web.config" file. its cause of well done running code sometimes...

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.