的app.config / web.config文件中的变量 [英] Variables within app.config/web.config

查看:221
本文介绍了的app.config / web.config文件中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时,可以做这样的事情在的app.config 的web.config 文件下面?

Is it is possible to do something like the following in the app.config or web.config files?

<appSettings>
 <add key="MyBaseDir" value="C:\MyBase" />
 <add key="Dir1" value="[MyBaseDir]\Dir1"/>
 <add key="Dir2" value="[MyBaseDir]\Dir2"/>
</appSettings>

我再想通过简单地说,访问方向2在我的code:

I then want to access Dir2 in my code by simply saying:

 ConfigurationManager.AppSettings["Dir2"]

这会帮助我,当我安装我在不同的服务器和地点的应用,其中我将只需要改变我的整个一个条目的app.config
(我知道我可以管理code中所有的串联,但我preFER这种方式)。

This will help me when I install my application in different servers and locations wherein I will only have to change ONE entry in my entire app.config. (I know I can manage all the concatenation in code, but I prefer it this way).

推荐答案

好问题。

我不觉得有什么。我相信它会如果有一个简单的方法已经颇有名气了,我看到微软正在创造Visual Studio 2010中一个机制,部署和测试部署不同的配置文件。

I don't think there is. I believe it would have been quite well known if there was an easy way, and I see that Microsoft is creating a mechanism in Visual Studio 2010 for deploying different configuration files for deployment and test.

随着中说,然而,我发现,您在的ConnectionStrings 部分有一种占位的所谓| DataDirectory目录|。也许你可以看看在工作中有什么有...

With that said, however; I have found that you in the ConnectionStrings section have a kind of placeholder called "|DataDirectory|". Maybe you could have a look at what's at work there...

下面是从的machine.config 一块显示它:

 <connectionStrings>
    <add
        name="LocalSqlServer"
        connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
        providerName="System.Data.SqlClient"
    />
 </connectionStrings>

这篇关于的app.config / web.config文件中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆