.net - 如何真正更改 MSBuild 输出/消息语言?

标签 .net visual-studio-2015 msbuild uiculture

TL;博士 :是否可以使用 Visual Studio 2015 的 MSBuild 在命令行上使用直接方法更改 MSBuild 的消息语言,而不是弄乱系统设置?如何?

我们有一个 question从“那时的方式”开始,这基本上是重复的,但恕我直言,答案表明该问题需要改进。

我需要它在 VS2015 及更高版本上工作,所以我不太关心旧版本。

所以,这里有一些事实:

  • 谷歌搜索没有出现任何 MSBuild command line选项将更改输出消息的语言。
  • 在德语系统上从命令行使用 MSBuild 会给我德语错误消息。
  • 在同一系统上使用英文 Visual Studio 来调用 MSBuild 将使 MSBuild(实际 MSBuild.exe 从进程浏览器进程树中确认)在德文系统上输出英文消息!
  • 这意味着 devenv.exe能开车 MSBuild.exe使用不同的语言。

  • 以机智:
    c:\temp\TestApp>msbuild TestApp.sln /t:rebuild /v:normal
    
    Microsoft (R)-Buildmodul, Version 14.0.25420.1
    Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
    
    Die Projekte in dieser Projektmappe werden nacheinander erstellt. Um eine parallele Erstellung zu ermöglichen, müssen Sie den Schalter "/m"
    hinzufügen.
    Der Buildvorgang wurde am 18.10.2016 11:06:33 gestartet.
    Projekt "c:\temp\TestApp\TestApp.sln" auf Knoten "1", rebuild Ziel(e).
    ValidateSolutionConfiguration:
      Die Projektmappenkonfiguration "Debug|X64" wird erstellt.
    Das Projekt "c:\temp\TestApp\TestApp.sln" (1) erstellt "c:\temp\TestApp\TestApp\TestApp.csproj" (2) auf Knoten "1", Rebuild Ziel(e).
    CoreClean:
      Die Datei "C:\temp\TestApp\TestApp\bin\x64\Debug\TestApp.exe.config" wird gelöscht.
      ...
    GenerateBindingRedirects:
      Keine vorgeschlagenen BindingRedirect-Einträge von ResolveAssemblyReferences.
    GenerateTargetFrameworkMonikerAttribute:
    Das Ziel "GenerateTargetFrameworkMonikerAttribute" wird übersprungen, da alle Ausgabedateien hinsichtlich der Eingabedateien aktuell sind.
    CoreCompile:
    ...
    

    来自 devenv :
    c:\temp\TestApp>devenv.com /Rebuild "Debug|x64" TestApp.sln
    
    Microsoft Visual Studio 2015 Version 14.0.25420.1.
    Copyright (C) Microsoft Corp. All rights reserved.
    1>------ Rebuild All started: Project: TestApp, Configuration: Debug x64 ------
    1>Build started 18.10.2016 11:10:27.
    1>CoreClean:
    1>  Deleting file "C:\temp\TestApp\TestApp\bin\x64\Debug\TestApp.exe.config".
    ...
    1>CoreCompile:
    ...
    

    显然,devenv 驱动了此处的输出,但实际的 msbuild 消息是相同的,并且 devenv 将调用 MSBuild.exe 作为实际的子进程。

    我已经检查过进程资源管理器:调用的 MSBuild.exe 没有任何命令行参数指示正在使用的项目,也没有任何明显的环境变量集。因此,devenv 似乎在“告诉” MSBBuild 通过其他方式使用英语作为语言。

    旁注 MSBuild 是从 VS 调用的,如下所示:C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe /nologo /nodemode:1 /nodeReuse:true
    那么,我该如何告诉 MSBuild.exe那它应该是英文输出吗?我对任何包装方法都很好,或者可能有一些 powershell 魔法( though that seems unlikely ),但我不能为此依赖机器/用户区域设置。

    另外,我找到了 an MSDN thread答案是——8 年前——VS 可以做到这一点,因为它设置了 UI 文化——但如果 devenv 可以做到这一点,因为它被调用了 msbuild.exe 我会,尽管我可能也可以:我只是没有线索如何。

    最佳答案

    我最终得到了下面的解决方案。不知道代码是否绝对正确(我不是 .NET 开发人员),但它运行 msbuild 并设置了“en-us”语言环境。

    class Program : MarshalByRefObject
    {
        static void Main(string[] args)
        {
            var newDomain = AppDomain.CreateDomain("enUSDomain");
            Program newProgram = (Program)newDomain.CreateInstanceAndUnwrap(
                    typeof(Program).Assembly.FullName,
                    typeof(Program).FullName);
    
            newProgram.Execute(args);
        }
    
        public void Execute(string[] args)
        {
            System.Globalization.CultureInfo.DefaultThreadCurrentUICulture =
                new System.Globalization.CultureInfo("en-US");
    
            byte[] bytes = File.ReadAllBytes("C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe");
            Assembly assembly = Assembly.Load(bytes);
            MethodInfo main = assembly.EntryPoint;
    
            main.Invoke(null, new object[] { });
        }
    }
    

    关于.net - 如何真正更改 MSBuild 输出/消息语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40104342/

    相关文章:

    c# - string.Replace 不适用于报价

    msbuild - 如何解决 : Custom MSBuild task requires assembly outside of AppBase

    c# - 编译器错误 Visual Studio mac

    c# - 如何识别大型项目中的循环引用

    c# - 无法在 VS 2015 内存使用分析器中找到预期的对象

    msbuild - NAnt 或 MSBuild,在 svn 环境中我应该使用哪一个?

    visual-studio - MSBuild->(箭头/破折号/->)运算符做什么?

    .net - 通过 C++ dll 调用时如何拆分 Dot Net Hosting 函数

    c# - 为什么 [.\r\n]* 正则表达式不匹配所有内容?

    c# - ASP.NET db.savechange System.Data.Entity.Infrastruct.DbUpdateException