的app.config [英] app.config

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

问题描述

我在VS2003.NET中使用C#。


我使用app.config将连接字符串存储到我的SQL Server。这个

工作正常。


最近我一直在工作回家,连接字符串是不同的b
。我通过app.config.home和

app.config.work文件解决了这个问题,我根据需要复制了app.config文件
我的位置
。 br />

我想知道是否有另一种方法可以做到这一点。在我的Microsoft.NET

文件系统层次结构中,我注意到一个CONFIG目录,其中包含machine.config

文件。所说的machine.config文件有一个示例appSettings部分




这是存放我的SQL Server连接的合适位置

字符串?我该如何访问它?我现在说

''ConfigurationSettings.AppSettings [" strCon"]''。怎么会改变?


谢谢你,

- 瑞克

I''m working in VS2003.NET, with C#.

I use app.config to store the connection string to my SQL Server. This
works fine.

Lately I''ve been taking work home where the connection string is
different. I deal with this by having app.config.home and
app.config.work files which I copy over the app.config file as required
by my location.

I was wondering if there is another way to do this. In my Microsoft.NET
file system hierarchy I notice a CONFIG directory with a machine.config
file in it. Said machine.config file has an example appSettings section
in it.

Would that be an appropriate place to store my SQL Server connection
string? How would I access it? I currently say
''ConfigurationSettings.AppSettings["strCon"]''. How would that change?

Thank you,

-- Rick

推荐答案

Rick,

我会推荐一个app.config文件。在这个单独的app.config

中有自定义部分,用于标识不同的环境(家庭或工作)。


有一个预定义的configSections部分您使用的app.config

来定义新的部分。


类似于:


< configuration> ;


< configSections>

< section name =" myClass1Stuff"

type =" System.Configuration。 DictionarySectionHandle r,System" />

< sectionGroup name =" environments">

< section name =" work"

type =" ; System.Configuration.SingleTagSectionHandler,System" />

< section name =" home"

type =" System.Configuration.SingleTagSectionHandler,System" />

< / sectionGroup>

< / configSections>


< appSettings>

< add key =" key1"值= QUOT; VALUE1" />

< add key =" environment"值= QUOT;工作" />

< / appSettings>


< myClass1Stuff>

< add key =" key1"值= QUOT; VALUE1" />

< / myClass1Stuff>


< environments>

< work value1 =" xyz"值2 =" ABC"值3 =" EDF" VALUE4 = QUOT; GHI" />

< home value1 =" xyz"值2 =" ABC"值3 =" EDF" VALUE4 = QUOT; GHI" />

< / environments>


< / configuration>


如果从DictionarySectionHandler继承您可以轻松更改钥匙

或您所在部分的价值。我从DictionarySectionHandler派生到

在我的一些项目中更改键/值以插入/输入。


我使用上面的环境部分来定义我的每个环境,

生产,质量保证,测试,开发。每个环境指向

正确的数据库服务器,Web服务器,消息队列,打印机任何'/

环境'设置。 appSettings / environment设置用于确定当前使用的环境
。而不是value1,value2,value3,

value4。我会有connectionString,printer,messageQueue等每个环境不同的




然后你需要使用System.Configuration.ConfigurationSettings.GetConf ig来

得到你的部分,它根据部分的类型返回一个对象

为该部分定义的处理程序(通常是HashTable,但可以是其他对象

类型。


请参阅以下有关如何通过configSections

部分创建新部分的信息。

http:// msdn .microsoft.com / library / de ... onhandlers.asp


和:
http://msdn.microsoft.com/library/de .. .ionsschema.asp


另外rea d关于System.Configuration.ConfigurationSettings类和

System.Configuration命名空间中的其他类。


如果你定义了一个足够明确的自定义部分,你可以放置它在

机器配置中,但我会把它放在除了
appSettings之外的其他东西,因为所有应用程序都可以读取appSettings(技术上所有.NET

应用程序可以读取机器配置的所有部分)。如果我在machine.config中放置一个

自定义部分,我很可能会使用命名空间

作为自定义部分名称。对于上面我会定义一个

定制部分来保持我正在使用的环境,然后


希望这有帮助

Jay

吉尼斯曼 < GM *** @ dublin.com>在消息中写道

新闻:MP *********************** @ news.newsguy.com ...
Rick,
I would recommend a single app.config file. Within this single app.config
have custom sections that identify distinct ''environments'' (home or work).

There is a predefined configSections section in the app.config that you use
to define new sections.

Something like:

<configuration>

<configSections>
<section name="myClass1Stuff"
type="System.Configuration.DictionarySectionHandle r, System" />
<sectionGroup name="environments">
<section name="work"
type="System.Configuration.SingleTagSectionHandler , System" />
<section name="home"
type="System.Configuration.SingleTagSectionHandler , System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environment" value="work" />
</appSettings>

<myClass1Stuff>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments>
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySectionHandler you can easily change the key
or value in your section. I''ve derived from DictionarySectionHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any ''per
environment'' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3,
value4. I would have connectionString, printer, messageQueue and such that
are distinct per environment.

You then need to use System.Configuration.ConfigurationSettings.GetConf ig to
get your section, which returns an object based on the type of section
handler defined for that section (usually HashTable, but can be other object
types.

See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/de...onhandlers.asp

and:
http://msdn.microsoft.com/library/de...ionsschema.asp

Also read about the System.Configuration.ConfigurationSettings class and
other classes in the System.Configuration namespace.

If you define a distinct enough custom section you could put it in the
machine config, however I would put it under something other than
appSettings, as all applications can read appSettings (technically All .NET
applications can read all sections of machine config). If I were to put a
custom section in machine.config I would more than likely use the namespace
for my assembly as the custom section name. For the above I would define a
custom section to hold which environment I was using, then

Hope this helps
Jay
"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP***********************@news.newsguy.com...
我正在使用C#在VS2003.NET中工作。

我使用app.config将连接字符串存储到我的SQL Server。这个
工作正常。

最近我一直把工作带回家,连接字符串不同。我通过app.config.home和
app.config.work文件解决了这个问题,我根据需要在app.config文件中复制了我的位置。

我想知道是否有另一种方法可以做到这一点。在我的Microsoft.NET
文件系统层次结构中,我注意到一个CONFIG目录,其中包含machine.config
文件。所述machine.config文件中有一个示例appSettings部分


这是一个存储我的SQL Server连接的合适位置吗?我该如何访问它?我现在说
''ConfigurationSettings.AppSettings [" strCon"]''。怎么会改变?

谢谢你,
- Rick
I''m working in VS2003.NET, with C#.

I use app.config to store the connection string to my SQL Server. This
works fine.

Lately I''ve been taking work home where the connection string is
different. I deal with this by having app.config.home and
app.config.work files which I copy over the app.config file as required
by my location.

I was wondering if there is another way to do this. In my Microsoft.NET
file system hierarchy I notice a CONFIG directory with a machine.config
file in it. Said machine.config file has an example appSettings section
in it.

Would that be an appropriate place to store my SQL Server connection
string? How would I access it? I currently say
''ConfigurationSettings.AppSettings["strCon"]''. How would that change?

Thank you,

-- Rick



Jay,


也许我很密集,但我的目标是能够压缩一个项目,

把它带回家,然后不加改变地运行它。尽管我可以说,这个

解决方案只是将编辑内容的问题推到了其他地方。 (我

还没弄明白,但是:-))


你说:
Jay,

Perhaps I''m dense, but my objective is to be able to zip up a project,
take it home, and run it without change. As near as I can tell, this
solution just pushes the problem of what to edit to someplace else. (I
haven''t figured out where, yet :-) )

You say:
所有应用程序都可以阅读appSettings(技术上所有.NET
应用程序都可以读取机器配置的所有部分)。


实际上,这对我来说听起来很完美。在工作中,所有应用程序都需要

访问同一个SQL Server。同样在家里。我可以看到在更复杂的环境中哪里有更多的服务器会有问题。


那么,当我调用System.Configuration.AppSettins时[ section指的是

搜索section部分在两个地方:app.config和machine.config?


- Rick

文章< eZ ********** ***@TK2MSFTNGP11.phx.gbl>,
Ja ******** @ email .msn.com 说... Rick,
我推荐一个app.config文件。在这个单独的app.config中有自定义部分,用于识别不同的环境(家庭或工作)。

您使用的app.config中有一个预定义的configSections部分
定义新的部分。

类似于:

< configuration>

< configSections>
< section name =" myClass1Stuff"
type =" System.Configuration.DictionarySectionHandle r,System" />
< sectionGroup name =" environments">
< section name =" work"
type =" System.Configuration.SingleTagSectionHandler,System" />
< section name =" home"
type =" System.Configuration.SingleTagSectionHandler,System" />
< / sectionGroup>
< / configSections>

< appSettings>
< add key =" key1"值= QUOT; VALUE1" />
< add key =" environment"值= QUOT;工作" />
< / appSettings>

< myClass1Stuff>
< add key =" key1"值= QUOT; VALUE1" />
< / myClass1Stuff>

< environments>
< work value1 =" xyz"值2 =" ABC"值3 =" EDF" VALUE4 = QUOT; GHI" />
< home value1 =" xyz"值2 =" ABC"值3 =" EDF" VALUE4 = QUOT; GHI" />
< / environments>

< / configuration>

如果从DictionarySectionHandler继承,您可以轻松更改密钥
或值在你的部分。我从DictionarySectionHandler派生出来,在我的一些项目中将键/值更改为插件/类型。

我使用上面的环境部分来定义我的每个环境,环境设置。 appSettings / environment设置用于识别当前使用的环境。而不是value1,value2,value3,
value4。我会有connectionString,printer,messageQueue等,每个环境都是不同的。
all applications can read appSettings (technically All .NET
applications can read all sections of machine config).
Actually, that sounds perfect to me. At work, all applications need to
access the same SQL Server. Likewise at home. I can see where in a
more complex environment with more servers it would be a problem.

So then, when I call System.Configuration.AppSettins["section"] does it
search for "section" in both places: app.config and machine.config?

-- Rick
In article <eZ*************@TK2MSFTNGP11.phx.gbl>,
Ja********@email.msn.com says... Rick,
I would recommend a single app.config file. Within this single app.config
have custom sections that identify distinct ''environments'' (home or work).

There is a predefined configSections section in the app.config that you use
to define new sections.

Something like:

<configuration>

<configSections>
<section name="myClass1Stuff"
type="System.Configuration.DictionarySectionHandle r, System" />
<sectionGroup name="environments">
<section name="work"
type="System.Configuration.SingleTagSectionHandler , System" />
<section name="home"
type="System.Configuration.SingleTagSectionHandler , System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environment" value="work" />
</appSettings>

<myClass1Stuff>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments>
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySectionHandler you can easily change the key
or value in your section. I''ve derived from DictionarySectionHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any ''per
environment'' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3,
value4. I would have connectionString, printer, messageQueue and such that
are distinct per environment.



Rick,
所以然后,当我调用System.Configuration.AppSettins [" section"]时,它会搜索section部分。在两个地方:app.config和machine.config?


ConfigurationSettings首先读取machine.config,然后合并。在app.config中是什么

。据我所知,它实际读取配置文件一次,

并缓存从ConfigurationSettings.GetConfig返回的结果,

避免额外的持续阅读开销和搜索文件。


此合并允许app.config覆盖machine.config中的内容。这也是因为appSettings中的每件事物通常都是< add>,

它可以< remove>或者< clear>


我在回复的底部试图说的是,如果你使用

machine.config请勿使用appSettings。定义您自己的自定义部分,

类似于环境部分在我的示例中,只需确保为程序集的命名空间命名

。还要确保这个名称

遵循.NET指南行(company.TehcnologyName)。

http://msdn.microsoft.com/library/de...guidelines。 asp

最大限度地减少对未来.NET升级和其他类库的干扰

可以安装。


希望这有帮助

Jay

Guinness Mann < GM *** @ dublin.com>在留言中写道

新闻:MP ************************ @ news.newsguy.com ..周杰伦,

也许我很密集,但我的目标是能够拉上一个项目,把它带回家,然后不加改变地运行它。尽管我可以说,这个解决方案只是将编辑内容的问题推到了其他地方。 (我还没弄明白,但是:-)

你说:
So then, when I call System.Configuration.AppSettins["section"] does it
search for "section" in both places: app.config and machine.config?
ConfigurationSettings reads machine.config first, then it "merges" what is
in app.config. I understand that it physically reads the config files once,
and caches the results returned from ConfigurationSettings.GetConfig,
avoiding extra overhead of continually reading & searching the files.

This merging allows app.config to override what is in machine.config. It is
also part of the reason that every thing in appSettings is normally <add>,
it can be <remove> or <clear>

What I tried to say at the bottom of my response is if you use
machine.config DO NOT USE appSettings. Define your own custom section,
similar to the "environments" section in my example, just make sure you name
the section for the namespace of your assembly. Also make sure this name
follows the .NET guide lines (company.TehcnologyName).

http://msdn.microsoft.com/library/de...guidelines.asp

To minimize interfering with future .NET upgrades and other class libraries
that may be installed.

Hope this helps
Jay
"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. . Jay,

Perhaps I''m dense, but my objective is to be able to zip up a project,
take it home, and run it without change. As near as I can tell, this
solution just pushes the problem of what to edit to someplace else. (I
haven''t figured out where, yet :-) )

You say:
所有应用程序都可以读取appSettings(技术上所有.NET
应用程序可以读取机器配置的所有部分。)
all applications can read appSettings (technically All .NET
applications can read all sections of machine config).



实际上,这对我来说听起来很完美。在工作中,所有应用程序都需要访问相同的SQL Server。同样在家里。我可以看到在更复杂的环境中有更多服务器的地方会出现问题。

那么,当我调用System.Configuration.AppSettins [" section"]时,它会做到br />搜索部分在两个地方:app.config和machine.config?

- Rick

文章< eZ ************* @ TK2MSFTNGP11.phx.gbl>,
Ja********@email.msn.com 说...

Rick,
我建议使用一个app.config文件。在这个单独的
app.config中有自定义部分,用于标识不同的''环境''(主页或
工作)。
app.config中有一个预定义的configSections部分,你
用于定义新的部分。

类似于:

< configuration>

< configSections>
<部分name =" myClass1Stuff"
type =" System.Configuration.DictionarySectionHandle r,System" />
< sectionGroup name =" environments">
< section name =" work"
type =" System.Configuration.SingleTagSectionHandler,System" />
< section name =" home"
type =" System.Configuration.SingleTagSectionHandler,System" />
< / sectionGroup>
< / configSections>

< appSettings>
< add key =" key1"值= QUOT; VALUE1" />
< add key =" environment"值= QUOT;工作" />
< / appSettings>

< myClass1Stuff>
< add key =" key1"值= QUOT; VALUE1" />
< / myClass1Stuff>

< environments>
< work value1 =" xyz"值2 =" ABC"值3 =" EDF" VALUE4 = QUOT; GHI" />
< home value1 =" xyz"值2 =" ABC"值3 =" EDF" VALUE4 = QUOT; GHI" />
< / environments>

< / configuration>

如果从DictionarySectionHandler继承,您可以轻松更改
键或值在你的部分。我从DictionarySectionHandler派生出来,在我的一些项目中将键/值更改为插件/类型。

我使用上面的环境部分来定义我的每个环境,环境设置。 appSettings / environment设置用于识别当前使用的环境。而不是value1,value2,
value3,value4。我会有connectionString,printer,messageQueue和每个环境不同的
Rick,
I would recommend a single app.config file. Within this single app.config have custom sections that identify distinct ''environments'' (home or work).
There is a predefined configSections section in the app.config that you use to define new sections.

Something like:

<configuration>

<configSections>
<section name="myClass1Stuff"
type="System.Configuration.DictionarySectionHandle r, System" />
<sectionGroup name="environments">
<section name="work"
type="System.Configuration.SingleTagSectionHandler , System" />
<section name="home"
type="System.Configuration.SingleTagSectionHandler , System" />
</sectionGroup>
</configSections>

<appSettings>
<add key="key1" value="value1" />
<add key="environment" value="work" />
</appSettings>

<myClass1Stuff>
<add key="key1" value="value1" />
</myClass1Stuff>

<environments>
<work value1="xyz" value2="abc" value3="edf" value4="ghi" />
<home value1="xyz" value2="abc" value3="edf" value4="ghi" />
</environments>

</configuration>

If you inherit from DictionarySectionHandler you can easily change the key or value in your section. I''ve derived from DictionarySectionHandler to
change key/value to plugin/type in a few of my projects.

I use the above environments section to define each of my environments,
Production, QA, Test, Development. Where each environment points to the
correct database servers, web servers, message queues, printers any ''per
environment'' settings. The appSettings/environment setting is used to
identify the current environment in use. Instead of value1, value2, value3, value4. I would have connectionString, printer, messageQueue and such that are distinct per environment.



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

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