Build 有没有办法用C代码获取活动解决方案配置名称?

Build 有没有办法用C代码获取活动解决方案配置名称?,build,configuration,visual-studio-2017,Build,Configuration,Visual Studio 2017,我在Visual Studio中的UWP解决方案中有三种解决方案配置: 发展 登台 生产 每个都与配置文件中的不同web服务和身份验证提供程序关联。在我的代码中,我如何分辨哪个是哪个?在过去,我明确提供了定义常量,但现在必须有更好的方法。活动解决方案配置存储在根解决方案文件夹.vs目录下的.suo文件中。.suo文件有,这意味着您不能仅仅用文本操作工具解析它 但是,使用一个可以用来操作这些类型文件的工具,您可以轻松地获得活动的解决方案配置 这是我写的一个控制台应用程序。请根据您的情况随意调整代码

我在Visual Studio中的UWP解决方案中有三种解决方案配置:

发展 登台 生产
每个都与配置文件中的不同web服务和身份验证提供程序关联。在我的代码中,我如何分辨哪个是哪个?在过去,我明确提供了定义常量,但现在必须有更好的方法。

活动解决方案配置存储在根解决方案文件夹.vs目录下的.suo文件中。.suo文件有,这意味着您不能仅仅用文本操作工具解析它

但是,使用一个可以用来操作这些类型文件的工具,您可以轻松地获得活动的解决方案配置

这是我写的一个控制台应用程序。请根据您的情况随意调整代码:

using OpenMcdf;
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace GetActiveBuildConfigFromSuo
{
    internal enum ProgramReturnCode
    {
        Success = 0,
        NoArg = -1,
        InvalidFileFormat = -2
    }

    internal class Program
    {
        private const string SolutionConfigStreamName = "SolutionConfiguration";
        private const string ActiveConfigTokenName = "ActiveCfg";

        internal static int Main(string[] args)
        {
            try
            {
                ValidateCommandLineArgs(args);

                string activeSolutionConfig = ExtractActiveSolutionConfig(
                    new FileInfo(args.First()));

                throw new ProgramResultException(
                    activeSolutionConfig, ProgramReturnCode.Success);
            }
            catch (ProgramResultException e)
            {
                Console.Write(e.Message);
                return (int)e.ReturnCode;
            }
        }

        private static void ValidateCommandLineArgs(string[] args)
        {
            if (args.Count() != 1) throw new ProgramResultException(
                "There must be exactly one command-line argument, which " +
                "is the path to an input Visual Studio Solution User " +
                "Options (SUO) file.  The path should be enclosed in " +
                "quotes if it contains spaces.", ProgramReturnCode.NoArg);
        }

        private static string ExtractActiveSolutionConfig(FileInfo fromSuoFile)
        {
            CompoundFile compoundFile;

            try { compoundFile = new CompoundFile(fromSuoFile.FullName); }
            catch (CFFileFormatException)
            { throw CreateInvalidFileFormatProgramResultException(fromSuoFile); }

            if (compoundFile.RootStorage.TryGetStream(
                SolutionConfigStreamName, out CFStream compoundFileStream))
            {
                var data = compoundFileStream.GetData();
                string dataAsString = Encoding.GetEncoding("UTF-16").GetString(data);
                int activeConfigTokenIndex = dataAsString.LastIndexOf(ActiveConfigTokenName);

                if (activeConfigTokenIndex < 0)
                    CreateInvalidFileFormatProgramResultException(fromSuoFile);

                string afterActiveConfigToken =
                    dataAsString.Substring(activeConfigTokenIndex);

                int lastNullCharIdx = afterActiveConfigToken.LastIndexOf('\0');
                string ret = afterActiveConfigToken.Substring(lastNullCharIdx + 1);
                return ret.Replace(";", "");
            }
            else throw CreateInvalidFileFormatProgramResultException(fromSuoFile);
        }

        private static ProgramResultException CreateInvalidFileFormatProgramResultException(
            FileInfo invalidFile) => new ProgramResultException(
                $@"The provided file ""{invalidFile.FullName}"" is not a valid " +
                $@"SUO file with a ""{SolutionConfigStreamName}"" stream and an " +
                $@"""{ActiveConfigTokenName}"" token.", ProgramReturnCode.InvalidFileFormat);
    }

    internal class ProgramResultException : Exception
    {
        internal ProgramResultException(string message, ProgramReturnCode returnCode) 
            : base(message) => ReturnCode = returnCode;

        internal ProgramReturnCode ReturnCode { get; }
    }
}
下载DTE nuget包:EnvDTE.8.0.2 添加以下代码

EnvDTE.DTE DTE=Marshal.GetActiveObjectVisualStudio.DTE.15.0作为EnvDTE.DTE

var activeConfig=stringDTE.Solution.Properties.ItemActiveConfig.Value


请注意,配置存在于编译时,而代码当然在运行时运行。什么是“配置”?编译器常量?我知道这些宏可用于构建。我正在寻找编译器可以使用的东西。我们有一个“DEBUG”宏,但我没有一个名为“DEBUG”的配置。我需要的东西,我可以说,如果配置==生产。。。恩迪夫。