如何在.net应用程序*享应用程序配置?

时间:2021-01-30 03:51:45

I've got a relatively large .Net system that consists of a number of different applications. Rather than having lots of different app.config files, I would like to share a single configuration file between all the apps.

我有一个相对较大的.Net系统,它包含许多不同的应用程序。我想在所有应用程序之间共享一个配置文件,而不是拥有许多不同的app.config文件。

I would also like to have one version when developing on my machine, one version for someone else developing on their machine, one version for a test system and one version for a live system.

我还想在我的机器上开发一个版本,一个版本供其他人在他们的机器上开发,一个版本用于测试系统,一个版本用于实时系统。

Is there an easy way of doing this?

有这么简单的方法吗?

5 个解决方案

#1


6  

For large amounts of configuration which is needed by multiple applications, I would put this configuration into a central repository, e.g. a database, file in a common location.

对于多个应用程序所需的大量配置,我会将此配置放入*存储库,例如数据库,文件位于公共位置。

To use different versions of a configuration file for different environments, create a build configuration for each of the different environments and a config file named after the environment, e.g:

要为不同的环境使用不同版本的配置文件,请为每个不同的环境创建构建配置,并为环境命名的配置文件,例如:

production production.app.config test test.app.config

生产production.app.config测试test.app.config

You can then use a pre build event to copy the correct config over the default app.config in your project. This will then get copied to your output directory as normal.

然后,您可以使用预构建事件在项目的默认app.config上复制正确的配置。然后,这将正常复制到您的输出目录。

The pre build event would look similar to the above, just use $(Configuration) to get the appropriate file for the environment you want.

预构建事件看起来与上面类似,只需使用$(Configuration)来获取所需环境的相应文件。

You could combine this with the above to copy the overall build specific config files into each project.

您可以将其与上面的内容结合起来,将整个构建特定的配置文件复制到每个项目中。

#2


2  

You could use a Post-build event (Properties -> Build Events) on your "child" projects to copy a config file from a master project to others, like this:

您可以在“子”项目上使用Post-build事件(Properties - > Build Events)将配置文件从主项目复制到其他项目,如下所示:

copy /Y c:\path\to\master\project\app.config $(TargetPath).config
exit 0

(The "exit 0" as the last line prevents a build error).

(作为最后一行的“exit 0”可防止生成错误)。

To have separate config files for different build targets ("RELEASE", "DEBUG", etc), you can edit the .csproj file (or .vbproj) in NOTEPAD.EXE to add an AppConfig tag for each of the target groups, like this:

要为不同的构建目标(“RELEASE”,“DEBUG”等)创建单独的配置文件,可以编辑NOTEPAD.EXE中的.csproj文件(或.vbproj),为每个目标组添加AppConfig标记,例如这个:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>.\bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <AppConfig>debug.app.config</AppConfig>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>.\bin\Devel\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <AppConfig>release.app.config</AppConfig>
  </PropertyGroup>

Notice the new <AppConfig> tags present in each group.

请注意每个组中存在的新 标记。

#3


2  

Instead of adding <add> elements to your <appSettings> section of your config file, you can add a file= attribute to the <appSettings> element to tell it to load that data from a different file. You could then keep your common settings in that common file.

您可以向 元素添加file =属性,告诉它从其他文件加载该数据,而不是将 元素添加到配置文件的 部分。然后,您可以将常用设置保存在该公共文件中。

See appSettings Element (General Settings Schema) in MSDN Library.

请参阅MSDN Library中的appSettings元素(常规设置架构)。

#4


1  

It is possible to use NTFS symbolic links to share a .NET .config file. I've seen this used successfully in a solution comprising of an ASP.NET applicaton, console applications, and more.

可以使用NTFS符号链接共享.NET .config文件。我已经看到这在包含ASP.NET应用程序,控制台应用程序等的解决方案中成功使用。

#5


0  

You can also put the configuration settings into the machine.config to share them amongst multiple applications. This makes deployment more problematic though.

您还可以将配置设置放入machine.config以在多个应用程序之间共享它们。这使得部署更成问题。

#1


6  

For large amounts of configuration which is needed by multiple applications, I would put this configuration into a central repository, e.g. a database, file in a common location.

对于多个应用程序所需的大量配置,我会将此配置放入*存储库,例如数据库,文件位于公共位置。

To use different versions of a configuration file for different environments, create a build configuration for each of the different environments and a config file named after the environment, e.g:

要为不同的环境使用不同版本的配置文件,请为每个不同的环境创建构建配置,并为环境命名的配置文件,例如:

production production.app.config test test.app.config

生产production.app.config测试test.app.config

You can then use a pre build event to copy the correct config over the default app.config in your project. This will then get copied to your output directory as normal.

然后,您可以使用预构建事件在项目的默认app.config上复制正确的配置。然后,这将正常复制到您的输出目录。

The pre build event would look similar to the above, just use $(Configuration) to get the appropriate file for the environment you want.

预构建事件看起来与上面类似,只需使用$(Configuration)来获取所需环境的相应文件。

You could combine this with the above to copy the overall build specific config files into each project.

您可以将其与上面的内容结合起来,将整个构建特定的配置文件复制到每个项目中。

#2


2  

You could use a Post-build event (Properties -> Build Events) on your "child" projects to copy a config file from a master project to others, like this:

您可以在“子”项目上使用Post-build事件(Properties - > Build Events)将配置文件从主项目复制到其他项目,如下所示:

copy /Y c:\path\to\master\project\app.config $(TargetPath).config
exit 0

(The "exit 0" as the last line prevents a build error).

(作为最后一行的“exit 0”可防止生成错误)。

To have separate config files for different build targets ("RELEASE", "DEBUG", etc), you can edit the .csproj file (or .vbproj) in NOTEPAD.EXE to add an AppConfig tag for each of the target groups, like this:

要为不同的构建目标(“RELEASE”,“DEBUG”等)创建单独的配置文件,可以编辑NOTEPAD.EXE中的.csproj文件(或.vbproj),为每个目标组添加AppConfig标记,例如这个:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>.\bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <AppConfig>debug.app.config</AppConfig>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>.\bin\Devel\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <AppConfig>release.app.config</AppConfig>
  </PropertyGroup>

Notice the new <AppConfig> tags present in each group.

请注意每个组中存在的新 标记。

#3


2  

Instead of adding <add> elements to your <appSettings> section of your config file, you can add a file= attribute to the <appSettings> element to tell it to load that data from a different file. You could then keep your common settings in that common file.

您可以向 元素添加file =属性,告诉它从其他文件加载该数据,而不是将 元素添加到配置文件的 部分。然后,您可以将常用设置保存在该公共文件中。

See appSettings Element (General Settings Schema) in MSDN Library.

请参阅MSDN Library中的appSettings元素(常规设置架构)。

#4


1  

It is possible to use NTFS symbolic links to share a .NET .config file. I've seen this used successfully in a solution comprising of an ASP.NET applicaton, console applications, and more.

可以使用NTFS符号链接共享.NET .config文件。我已经看到这在包含ASP.NET应用程序,控制台应用程序等的解决方案中成功使用。

#5


0  

You can also put the configuration settings into the machine.config to share them amongst multiple applications. This makes deployment more problematic though.

您还可以将配置设置放入machine.config以在多个应用程序之间共享它们。这使得部署更成问题。