如何使用不同的.settings文件在.NET不同的环境? [英] How to use different .settings files for different environments in .NET?

查看:242
本文介绍了如何使用不同的.settings文件在.NET不同的环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET允许您使用.settings文件来管理应用程序设置。我想单独存放生产,开发和测试设置的方式,我可以做这样的事情:

.NET allows you to use .settings files to manage application settings. I would like to store Production, Development and Test settings separately in a way that I can do something like this:

EnvironmentSettings environmentSettings;

// get the current environment (Production, Development or Test)
ApplicationEnvironment Environment = (ApplicationEnvironment)
    Enum.Parse(typeof(ApplicationEnvironment), Settings.Default.ApplicationEnvironment);

switch (Environment)
{
    case ApplicationEnvironment.Production:
        environmentSettings = Settings.Production;
        break;
    ...
}

string reportOutputLocation = environmentSettings.ReportOutputLocation;

基本上,我想两个不同的设置类:存储选定的环境和非环境的具体性能,一般设置类,以及被称为第二类EnvironmentSettings 3静态实例。使用的实例应该依赖于在一般设置类指定的环境。

Basically, I want two separate Settings classes: the general Settings class that stores the selected environment and non-environment specific properties, and 3 static instances of a second class called EnvironmentSettings. The instance used should depend on the environment specified in the general Settings class.

有没有办法做到这短短的手动设置的静态构造函数或东西,所有这些设置的值?如果我不得不这样做,我宁愿只是有一个很大的设置类,如DevOutputLocation,LiveOutputLocation等属性。

Is there any way to do this short of manually setting the values for all these settings in a static constructor or something? If I have to do that, I'd rather just have one big Settings class with properties like "DevOutputLocation", "LiveOutputLocation", etc.

我可以有多个.settings文件在我的项目,但只是创建单独的类不从对方获得。所以,我可以让DevelopmentSettings.settings,ProductionSettings.settings和TestSettings.settings文件,并设置相同的属性,但我需要一堆switch语句随处可见,以确定要使用,因为它们不从一个普通类派生的类

I can have multiple .settings files in my project but that just creates separate classes that don't derive from each other. So I could make DevelopmentSettings.settings, ProductionSettings.settings and TestSettings.settings files and give them the same properties, but then I would need a bunch of switch statements everywhere to determine which class to use because they don't derive from a common class.

推荐答案

我使用这种方法来.config文件,我相信它会帮助你。 只要看看在<一个href="http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWith$p$pBuildEvents.aspx"相对=nofollow>斯科特Hanselman的博客帖子和<一href="http://stackoverflow.com/questions/1472550/multiple-$p$p-build-events-in-visual-studio/1472841">this问题 。意识形态是简单的像地狱,但工程pretty的好。所有你需要的是:

I am using this approach for .config files, I am sure that it will help you too. Just look on Scott Hanselman's blog post and this question. Ideology is simple like hell, but works pretty good. All you will need is:

  1. 创建多个文件进行不同的配置
  2. 按照惯例,如他们的名字my.dev.settings,my.live.settings
  3. 创建后生成事件(见链接)
  4. 在享受=)

实例化$ C $下你类的设置总是会在默认设置(这将被替换后,每建立一个与需要的人),所以这种方式需要最少的努力。

Instantiation code for you class with settings will always look in default settings (which will be replaced after each build with the needed one), so this approach require minimal effort.

这篇关于如何使用不同的.settings文件在.NET不同的环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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