# Trying to set a valid path in Application Configuration in web.config of ASP.NET (same as C#)

Hi there;

I am trying to set a valid path value for Application Configuration in web.config of ASP.NET

I have a key which has a value as this:

<add key="foo" value="C:/baz/my/project/path/bar>

The thing is when I copy my project to another machine, it has no baz directory but my/project/path/bar directory since it's the project path itself. So, i need to refer a generic solution in web.config file.

What to do?

Regards.
LVL 12
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Independent ConsultantCommented:
The .NET Framework comes with a bunch of different methods you can use to get the path relative to the running application. The one you might be interested in here is this:

http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.100).aspx

However, that might not do it. You could also check out:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly(v=vs.100).aspx

Regardless, there should be something that meets your needs.
0
Author Commented:
Ah, i guess you misunderstood me.

I need to change the web.xml, not the calling code.

I was asking what i should write as a value in web.config.

add key="var" value="stg generic reflecting the project base"

In my xml, there is the baz folder which works fine in my local machine but fails for remote machine since baz is not part of the project structure.

What to do in config file?

Regards.
0
Independent ConsultantCommented:
I did misunderstand. This isn't something you typically do, but it appears to be something that other have needed doing too:

http://msdn.microsoft.com/en-us/library/4c2kcht0(v=vs.85).aspx

This is an example:

http://www.dotnetperls.com/appsettings

I just realized these might not show how to SET the values. Here is an example:

http://msmvps.com/blogs/simpleman/archive/2005/06/23/54733.aspx

and here too:

http://blog.itsyssolutions.com/modify-webconfig-file-programmatically-at-runtime/
0
Author Commented:
Above is not totally helping..Ah, the problem is actually like this.

http://stackoverflow.com/questions/7712450/how-to-pass-relative-path-in-appsetting

What should i do for web.config file?

Regards.
0
Independent ConsultantCommented:
I looked at the Stack Overflow post, but I am now less sure of what you want. At first, I thought you wanted the ability to find out which directory the application was running from. Then, I thought you wanted the ability to modify a setting in the web.config programmatically. Now, I'm not sure what you want. Can you provide more specifics?

Maybe explain what you're trying to do, exactly?
0
Author Commented:
Hi;

My mistake, I create a confusion i think.

What I want is that I need to set a relative path for my project in my web.config in xml. What is the way for this?

Regards.
0
Independent ConsultantCommented:
I guess... a combination of the two methods described?

1. You know the basis for your path: "\my\project\path\bar".
2. You know you need a relative path based on this basis: "~\my\project\path\bar".
3. You need the actual path based off the relative path(??):
var physicalPath = Server.MapPath(@"~\my\project\path\bar");

4. Now, you need to add it to the Web.Config (??):

Configuration configFile = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection AppSection = configFile.GetSection("appSettings") as AppSettingsSection;
configFile.Save();


I think this is what you want?
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Hi;

You really brute force, thank you. :)

in web.config, may i?