我有一个解决方案,其中包括一个asp.net-mvc-2项目、一个Web服务、一个类库和三个常规桌面应用程序。目前,我需要将所有app.config设置从一个项目复制到另一个项目。如果我想做出更改,那将是一场噩梦。
我知道有一种解决方法可以将所有这些信息存储在全局的 .config 文件中,但我正在寻找一种只将值存储在我的解决方案中而不是我的机器上的解决方案。这是否可能?
我有一个解决方案,其中包括一个asp.net-mvc-2项目、一个Web服务、一个类库和三个常规桌面应用程序。目前,我需要将所有app.config设置从一个项目复制到另一个项目。如果我想做出更改,那将是一场噩梦。
我知道有一种解决方法可以将所有这些信息存储在全局的 .config 文件中,但我正在寻找一种只将值存储在我的解决方案中而不是我的机器上的解决方案。这是否可能?
CommonSettings.config
的文件。
2. 将您的公共设置放在此文件中:
<appSettings>
<add key="someCommonSetting" value="some value"></add>
<!-- more setting here -->
</appSettings>
<appSettings file="CommonSettings.config">
<add key="Value1" value="123" />
</appSettings>
For this approach to work, the shared config file should be copied to the project’s output directory so it is adjacent to the regular App/Web.config file. Add the existing shared .config file to the project as a Linked file and set it to ‘Copy if newer’. You should see something similar to this in your .csproj file:
<None Include="..\CommonConnectionStrings.config"> <Link>CommonConnectionStrings.config</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None>
ConnectionString
进行相同的操作,在CommonConnectionStrings.config
中复制所有连接字符串,在其他的App.Config/Web.Config中像这样引用它:<connectionStrings configSource="CommonConnectionStrings.config" />
您可以将一个.config文件添加到一个项目中,然后选择添加 -> 现有项目... -> 添加为链接来将对该文件的引用添加到其他项目中。它们都将使用该文件进行构建,就好像它是它们自己的副本一样,但实际上只有一个副本,而且您只需要在一个地方进行更改。