.NET 不同的应用程序设置用于开发和发布

发布于 2024-08-25 02:05:30 字数 121 浏览 2 评论 0原文

我正在使用 VS2010 C#.NET 3.5 和应用程序设置(Settings.settings 文件)。我想要做的是为我的开发和生产环境设置不同的设置,而不必在代码中使用检查调试模式的条件语句。解决这个问题的常见方法是什么?

I am using VS2010 C#.NET 3.5 and application settings (the Settings.settings file). What I want to do is have different settings for my development and production environments without having to litter my code with conditional statements checking for debug mode. What is the common approach to this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

天涯离梦残月幽梦 2024-09-01 02:05:30

或者,您可以创建单独的配置文件并调用它们:

Release.settings

Debug.settings

,然后设置条件构建事件,将相应的 .config 文件复制到 Settings.Settings


if $(ConfigurationName) == Debug xcopy debug.settings settings.settings
if $(ConfigurationName) == Release xcopy release.settings settings.settings

Or you can just create separate config files and call them:

Release.settings

Debug.settings

then setup conditional build events that copy the appropriate .config file to Settings.Settings


if $(ConfigurationName) == Debug xcopy debug.settings settings.settings
if $(ConfigurationName) == Release xcopy release.settings settings.settings

温馨耳语 2024-09-01 02:05:30

假设您指的是设置的值,我认为最好的方法是将所有生产环境值放在设置文件的注释中。或者仅在部署应用程序时设置它们。

如果这就是您所追求的,.Net 中没有真正的内置机制。

在我目前的工作中,我们有一个小的命令行工具,可以为给定的环境设置正确的所有值。配置是在数据库中完成的。

在我之前的工作中,我们从未将配置文件复制到部署目录,而是在需要时手动更改文件。

Assuming you mean the values of the settings, I think the best way to do this is to put all the production environment values in a comment in the settings file. Or only set them when deploying the application.

There's no real built-in mechanism for this in .Net if that's what you're after.

At my current job we have a little command-line tool which will set all the values right for a given environment. The configuration is done in a database.

At my previous job we never copied the config file to the deployment directory, but rather changed the files manually if needed.

夜无邪 2024-09-01 02:05:30

我们永远不会覆盖生产系统上的 web.config 文件。如果添加其他设置/键,我们会在发布时粘贴它们。

We never override our web.config file on the production systems. If additional settings/keys are added, we paste them in at the time of publishing.

满身野味 2024-09-01 02:05:30

如果您使用 Web 部署项目,则可以对其进行设置,以便替换 web.config 的不同部分。您可以在这里阅读有关它们的更多信息:http://weblogs。 asp.net/scottgu/archive/2005/11/06/429723.aspx

我们在当前工作中实际采用的另一种方法是拥有多个 web.config 文件 - 每个环境 1 个(例如 web.config、web.config.Production)。然后,当我们构建部署时,我们使用 msbuild 自动将 web.config.Production 交换为 web.config。

If you use web deployment projects, you can set it up so that different parts of your web.config will be replaced. You can read more about them here: http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx.

Another approach, that we actually take at my current job is to have multiple web.config files - 1 for each environment (e.g. web.config, web.config.production). Then when we build for deployment, we use msbuild to automatically swap web.config.production in as the web.config.

黒涩兲箜 2024-09-01 02:05:30

我认为这是缺少xxx.config文件。这就是为什么我使用自己的配置管理器,它可以在安装/执行应用程序的每台计算机上使用不同的配置。

I think this is the lack of xxx.config files. That's why I use own config manager which can be used with different configurations on each machine where application is installed/executed.

自由范儿 2024-09-01 02:05:30

我们使用 NAnt 脚本来构建我们的解决方案,替换配置文件中的设置值,运行测试并发布。

一旦你设置了一两次,它就相当简单了。

we use a NAnt script to build our solution, replace setting values in the config file, run tests and publish.

It's fairly straight forward once you've set it up once or twice.

夏末的微笑 2024-09-01 02:05:30

您可以有条件地在构建过程中包含项目,例如

<ItemGroup Condition=" '$(Configuration)' == 'Debug' " ...

请参阅 https://stackoverflow.com/a/2093289/254109

You can conditionally include items in build process, like

<ItemGroup Condition=" '$(Configuration)' == 'Debug' " ...

see https://stackoverflow.com/a/2093289/254109

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文