如何在运行时获取构建配置?

时间:2009-05-06 12:13:44

标签: c# configuration

有谁知道如何在C#代码中获取当前的构建配置$(Configuration)

7 个答案:

答案 0 :(得分:15)

你不能,不是真的。 您可以做的是定义一些“条件编译符号”,如果查看项目设置的“构建”页面,可以在那里设置它们,这样就可以编写#if语句来测试它们。

为调试版本自动注入DEBUG符号(默认情况下,可以关闭它)。

所以你可以写这样的代码

#if DEBUG
        RunMyDEBUGRoutine();
#else
        RunMyRELEASERoutine();
#endif

但是,除非你有充分的理由,否则不要这样做。在调试和发布版本之间使用不同行为的应用程序对任何人都没有好处。

答案 1 :(得分:13)

如果您卸载项目(在右键菜单中)并在</Project>标记之前添加它,它将保存一个包含您配置的文件。然后,您可以将其重新读入以便在代码中使用。

<Target Name="BeforeBuild">
    <WriteLinesToFile File="$(OutputPath)\env.config" 
                      Lines="$(Configuration)" Overwrite="true">
    </WriteLinesToFile>
</Target>

答案 2 :(得分:6)

条件编译符号可用于实现此目的。您可以定义属性&gt;的自定义符号。为每个项目构建设置窗格,并使用#if指令在代码中测试它们。

示例说明如何定义符号UNOEURO以及如何在代码中使用它。

UNOEURO symbol defined here

bool isUnoeuro = false;
#if UNOEURO
    isUnoeuro = true;
#endif

答案 3 :(得分:4)

.NET中有AssemblyConfigurationAttribute。您可以使用它来获取构建配置的名称

var assemblyConfigurationAttribute = typeof(CLASS_NAME).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>();
var buildConfigurationName = assemblyConfigurationAttribute?.Configuration;

答案 4 :(得分:2)

  1. 安装enter image description here Visual Studio扩展。
  2. 右键单击您的配置文件,然后选择“添加转换”。

SlowCheetah

  1. 请注意每个构建配置的转换。

Add Transform

  1. 在根配置文件中放置一个“构建” appSetting:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
        </startup>
      <appSettings>
        <add key="Build" value="" />
      </appSettings>
    </configuration>
  1. 并在每个转换中放置一个“ Build”指令:
    <?xml version="1.0" encoding="utf-8"?>
    <!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <appSettings>
        <add key="Build" value="Debug" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
      </appSettings>
    </configuration>
  1. 然后在您的C#代码中获取“构建” appSetting值:

ConfigurationManager.AppSettings["Build"]


  • 请注意,您在调试时将不会看到转换:Transform for each build configuration

答案 5 :(得分:0)

我不相信你可以在编译时将它注入到程序集中,但是你可以实现它的一种方法是使用MSBuild并将其添加到应用程序的配置文件中。

请参阅此博客文章,了解如何使用MSBuild执行多环境配置文件 - http://adeneys.wordpress.com/2009/04/17/multi-environment-config/

或者你可以编写一个MSBuild任务来编辑某个编译文件(你的C#或VB文件)并在BeforeBuild任务中运行。它是相当棘手的,因为你需要找出将它注入文件的位置,但如果你设置了某种标记化设置,你应该能够做到这一点。我也怀疑它会很漂亮!

答案 6 :(得分:0)

您可以使用带条件属性的常见静态方法来设置标志以检测DEBUG或RELEASE模式。仅当在DEBUG模式下运行时,才调用SetDebugMode方法,否则它将被运行时忽略。

public static class AppCompilationConfiguration
{
    private static bool debugMode;

    private static bool IsDebugMode()
    {
        SetDebugMode();
        return debugMode;
    }

    //This method will be loaded only in the case of DEBUG mode. 
    //In RELEASE mode, all the calls to this method will be ignored by runtime.
    [Conditional("DEBUG")]
    private static void SetDebugMode()
    {
        debugMode = true;
    }

    public static string CompilationMode => IsDebugMode() ? "DEBUG" : "RELEASE";

}

您可以在下面的代码中调用它

 Console.WriteLine(AppCompilationConfiguration.CompilationMode);