有没有办法在C#代码中获取Active Solution Configuration名称?

问题描述 投票:1回答:1

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

  • 发展
  • 分期
  • 生产

每个都与配置文件中的不同Web服务和身份验证提供程序相关联。在我的代码中,我如何判断哪一个是哪个?在过去,我已明确提供了DEFINE常量,但现在必须有更好的方法。

build configuration visual-studio-2017
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; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.