有没有办法在 C# 代码中获取活动解决方案配置名称



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

  • 发展
  • 分期
  • 生产

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

活动解决方案配置存储在根解决方案文件夹的 .vs 目录下的.suo文件中。 .suo文件具有复合文件二进制格式,这意味着您不能仅使用文本操作工具对其进行解析。

但是,使用 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('');
                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; }
    }
}
  1. 下载 DTE nuget 软件包 : EnvDTE.8.0.2
  2. 添加以下代码

    EnvDTE.DTE DTE = Marshal.GetActiveObject("VisualStudio.DTE.15.0"( as EnvDTE.DTE;

    var activeConfig = (string(DTE.Solution.Properties.Item("ActiveConfig"(.价值;

相关内容

最新更新