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

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

问题描述

是否可以在 app.configweb.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>

然后我想通过简单地说来访问我的代码中的 Dir2:

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

 ConfigurationManager.AppSettings["Dir2"]

当我在不同的服务器和位置安装我的应用程序时,这对我很有帮助,我只需要更改整个 app.config 中的一个条目.(我知道我可以在代码中管理所有连接,但我更喜欢这种方式).

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).

推荐答案

好问题.

我认为没有.我相信,如果有一种简单的方法,它会是众所周知的,而且我看到 Microsoft 在 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 的一段展示:

Here's a piece from machine.config showing it:

 <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天全站免登陆