App.Config文件 [英] App.Config file

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

问题描述




有没有办法在app.config文件中有条件语句?

我想添加键,所以基于args [],从配置文件中取出适当的密钥

并在我的应用程序中使用


谢谢,

Stephen。

Hi,

Is there a way to have conditional statements in app.config file?
I want to add keys, so that based on the args[], appropriate keys are taken
from the config file and used in my app

Thanks,
Stephen.

推荐答案

我无法回答ASP.NET 2.0。但是对于ASP.NET 1.1,您可以在配置文件中定义

自定义部分,然后将自定义部分

名称传递给您的方法,并从中获取值适当的部分。


所以,你的web.config可能看起来像这样(自定义

部分的定义,然后是自定义部分):


< configuration>

< configSections>

< section

name =" dev" type =" System.Configuration.NameValueFileSectionHan dler,

System,Version = 1.0.5000.0,Culture = neutral,

PublicKeyToken = b77a5c561934e089" />

< section

name =" stage" type =" System.Configuration.NameValueFileSectionHan dler,

System,Version = 1.0.5000.0,Culture = neutral,

PublicKeyToken = b77a5c561934e089" />

< section

name =" prod" type =" System.Configuration.NameValueFileSectionHan dler,

System,Version = 1.0.5000.0,Culture = neutral,

PublicKeyToken = b77a5c561934e089" />

< / configSections>


< dev>

< add key =" AppRootDir" value =" / Authoring /" />

< / dev>


< stage>

< ; add key =" AppRootDir" value =" / S-Authoring /" />

< / stage>


< prod>

< add key =" AppRootDir" value =" / P-Authoring /" />

< / prod>

< / configuration>


然后,编写一个按部分重复值的类,使用调用

,例如:


NameValueCollection nvc =

(NameValueCollection)ConfigurationSettings.GetConf ig(" dev");


- 然后返回你想要的值:


string AppRootDir = nvc [" AppRootDir"]; //返回" /创作/"


- 希望这有帮助

I can''t answer for ASP.NET 2.0. But for ASP.NET 1.1, you can define
custom sections in the config file, then, pass in the custom section
name to your method, and pluck the value from the appropriate section.

So, your web.config might look like this (definition of the custom
sections, followed by custom sections):

<configuration>
<configSections>
<section
name="dev" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>

Then, write a class that retrives the values by section, using a call
such as:

NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig("dev");

-- then return the value you want:

string AppRootDir = nvc["AppRootDir"]; //returns "/Authoring/"

-Hope this helps


是的,< br $> b $ b非常感谢

Stephen


" KJ" < N _ ********** @ mail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
Yes it does,
Thanks a lot
Stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
我无法回答ASP.NET 2.0。但是对于ASP.NET 1.1,您可以在配置文件中定义
自定义部分,然后将自定义部分
名称传递给您的方法,并从相应的部分中获取值。

所以,你的web.config可能看起来像这样(自定义
部分的定义,然后是自定义部分):

< configuration>
< configSections> ;
< section
name =" dev" type =" System.Configuration.NameValueFileSectionHan dler,
System,Version = 1.0.5000.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089" />
< section
name = QUOT;阶段" type =" System.Configuration.NameValueFileSectionHan dler,
System,Version = 1.0.5000.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089" />
< section
name = QUOT; PROD" type =" System.Configuration.NameValueFileSectionHan dler,
System,Version = 1.0.5000.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089" />
< / configSections>

< dev>
< add key =" AppRootDir" value =" / Authoring /" />
< / dev>
< stage>
< add key =" AppRootDir" value =" / S-Authoring /" />
< / stage>
< prod>
< add key =" AppRootDir" value =" / P-Authoring /" />
< / prod>
< / configuration>

然后,编写一个通过以下方式重写值的类部分,使用电话
如:NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig(" dev");

- - 然后返回你想要的值:

string AppRootDir = nvc [" AppRootDir"]; //返回" / Authoring /"

- 希望这有帮助
I can''t answer for ASP.NET 2.0. But for ASP.NET 1.1, you can define
custom sections in the config file, then, pass in the custom section
name to your method, and pluck the value from the appropriate section.

So, your web.config might look like this (definition of the custom
sections, followed by custom sections):

<configuration>
<configSections>
<section
name="dev" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>

Then, write a class that retrives the values by section, using a call
such as:

NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig("dev");

-- then return the value you want:

string AppRootDir = nvc["AppRootDir"]; //returns "/Authoring/"

-Hope this helps



嗨KJ,


我在读取app.config文件时收到错误

Version = 1.0.5000.0,Culture = neutral,PublicKeyToken = b77a5c561934e089"


我做错了什么?

我使用的是1.1.4322版本,但在哪里可以找到publickeytoken?


再次感谢,
Stephen


" KJ" < N _ ********** @ mail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
Hi KJ,

I am getting an Error while reading the app.config file at
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

what am i doing wrong?
I am using 1.1.4322 version but where can i find the publickeytoken?

Thanks again,
Stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
我无法回答ASP.NET 2.0。但是对于ASP.NET 1.1,您可以在配置文件中定义
自定义部分,然后将自定义部分
名称传递给您的方法,并从相应的部分中获取值。

所以,你的web.config可能看起来像这样(自定义
部分的定义,然后是自定义部分):

< configuration>
< configSections> ;
< section
name =" dev" type =" System.Configuration.NameValueFileSectionHan dler,
System,Version = 1.0.5000.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089" />
< section
name = QUOT;阶段" type =" System.Configuration.NameValueFileSectionHan dler,
System,Version = 1.0.5000.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089" />
< section
name = QUOT; PROD" type =" System.Configuration.NameValueFileSectionHan dler,
System,Version = 1.0.5000.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089" />
< / configSections>

< dev>
< add key =" AppRootDir" value =" / Authoring /" />
< / dev>
< stage>
< add key =" AppRootDir" value =" / S-Authoring /" />
< / stage>
< prod>
< add key =" AppRootDir" value =" / P-Authoring /" />
< / prod>
< / configuration>

然后,编写一个通过以下方式重写值的类部分,使用电话
如:NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig(" dev");

- - 然后返回你想要的值:

string AppRootDir = nvc [" AppRootDir"]; //返回" / Authoring /"

- 希望这有帮助
I can''t answer for ASP.NET 2.0. But for ASP.NET 1.1, you can define
custom sections in the config file, then, pass in the custom section
name to your method, and pluck the value from the appropriate section.

So, your web.config might look like this (definition of the custom
sections, followed by custom sections):

<configuration>
<configSections>
<section
name="dev" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>

Then, write a class that retrives the values by section, using a call
such as:

NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig("dev");

-- then return the value you want:

string AppRootDir = nvc["AppRootDir"]; //returns "/Authoring/"

-Hope this helps



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

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