有什么方法可以在C#代码中获取“活动解决方案配置”名称?

时间:2019-04-02 15:42:44

标签: build configuration visual-studio-2017

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

  • 发展
  • 分期
  • 生产

每个配置文件中都与另一个Web服务和身份验证提供程序关联。在我的代码中,我该如何分辨是哪一个?过去,我曾明确提供过DEFINE常数,但现在必须有更好的方法。

2 个答案:

答案 0 :(得分:2)

  1. 下载 DTE nuget软件包: EnvDTE .8.0.2
  2. 添加以下代码

    EnvDTE.DTE DTE = Marshal.GetActiveObject(“ VisualStudio.DTE.15.0”)作为EnvDTE.DTE;

    var activeConfig =(string)DTE.Solution.Properties.Item(“ ActiveConfig”)。Value;

答案 1 :(得分:0)

活动解决方案配置存储在根解决方案文件夹下.vs目录下的.suo文件中。 .suo文件具有a compound file binary format,这意味着您不能仅使用文本处理工具对其进行解析。

但是,使用OpenMcdf(可用于处理这些类型的文件的工具),您可以轻松地获得有效的解决方案配置。

这是我编写的可正常使用的控制台应用程序。随时根据您的情况调整代码:

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; }
    }
}