首页 > 编程语言 >C#完成项目编译

C#完成项目编译

时间:2023-02-05 17:11:43浏览次数:46  
标签:process string C# ConsoleApp1 编译 Build 完成 new

1、使用Microsoft.Build进行项目编译

        static void Build()
        {
            // 项目文件路径
            string projectFilePath = @"C:\Users\97460\source\repos\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj";
          
            // 方式1
            Project project = new Project(projectFilePath);
            if (project.Build())
            {
                Console.WriteLine("编译成功!");
            }
            else
            {
                Console.WriteLine("编译失败");
            }

            // 方式2
            using (ProjectCollection projectCollection = new ProjectCollection())
            {
                // 编译结果日志输出文件
                string buildResultFilePath = @"logfile=C:\\1.txt";
                ILogger logger = new Microsoft.Build.Logging.FileLogger()
                {
                    Parameters = buildResultFilePath,
                };

                // 编译全局全量
                var globalProperty = new Dictionary<string, string>();
                globalProperty.Add("Configuration", "Debug");        
                globalProperty.Add("Platform", "AnyCPU");
               
                // 编译
                var buidlRequest = new BuildRequestData(projectFilePath, globalProperty, null, new string[] { "Build" }, null);
                var buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(projectCollection)
                {
                    Loggers = new Collection<ILogger> { logger } 
                },buidlRequest);
                if (buildResult.OverallResult == BuildResultCode.Success)
                {
                    Console.WriteLine("编译成功!");
                }
                else
                {
                    Console.WriteLine("编译失败");
                }
            }
        }

2、使用devenv命令行进行编译

        static void BuildCmd()
        {
            // 添加devenv环境变量
            string vs2022 = "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\";
            var envirPaths = Environment.GetEnvironmentVariable("Path").Split(';');
            if (envirPaths.FirstOrDefault(e => e == vs2022) == null)
            {
                var newPath = $"{Environment.GetEnvironmentVariable("Path")};{vs2022}";
                Environment.SetEnvironmentVariable("Path", newPath);
            }

            // 项目文件路径
            string solutionFilePath = "C:\\Users\\97460\\source\\repos\\ConsoleApp1\\ConsoleApp1.sln";
            string projectFilePath = @"C:\Users\97460\source\repos\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj";

            // 启动编译命令
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WorkingDirectory = Path.GetDirectoryName(solutionFilePath);
            process.StartInfo.FileName = "cmd.exe";
            // 编译解决方案(Debug编译)
            process.StartInfo.Arguments = "/c " + $"devenv {solutionFilePath} /Build Debug";
            // 编译项目(Release编译)
            process.StartInfo.Arguments = "/c " + $"devenv {solutionFilePath} /Project {projectFilePath} /Build Release";
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            var buildResultInfo = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Close();
            Console.WriteLine(buildResultInfo);
        }

更多的devenv命令可阅读 Devenv 命令行开关 - Visual Studio (Windows) | Microsoft Learn

标签:process,string,C#,ConsoleApp1,编译,Build,完成,new
From: https://www.cnblogs.com/dongweian/p/17093613.html

相关文章

  • Colab_解决训练卡在第一个epoch且step不动的问题
    一.问题如果在使用colab时遇到卡在第一个epoch且step一动不动的情况,大概率是因为需要读取的数据集路径在你的谷歌网盘下,这样就需要联网读取数据到你的云端虚拟电脑中,会导......
  • 论文翻译:2022_PercepNet+: A Phase and SNR Aware PercepNet for Real-Time Speech En
    博客地址:凌逆战(转载请注明出处) 论文地址:PercepNet+:用于实时语音增强的相位和信噪比感知PercepNet引用格式: GeX,HanJ,LongY,etal.PercepNet+:APhasea......
  • Java多线程并发06—CAS、AQS
    CAS(CompareAndSwap/Set)概念CAS函数,是比较并交换函数,它是原子操作函数。原理CAS是基于乐观锁的原理进行操作的。它总是认为自己可以成功完成操作。当多个线程同时使用CAS......
  • PHP7 preg_replace 出错及解决办法
    问题描述:PHP7废弃了preg_replace?原本是中php5中处理url中后面参数替换清除的,代码如下$url=preg_replace('/([?&])src=[^&]+(&?)/e','"$2"==""?"":"$1"',$url);但......
  • 电商导购CPS,这是最容易上手的创业项目了吧
    大家好,我是小悟前段时间写了个外卖cps小程序,并开源了代码,源码在这里  零基础,搞外卖CPS小程序副业,附源码 ,有需要的小伙伴自取,可以结合这篇文章  外卖CPS小程序部署指南......
  • Spring—IOC 容器
    在如今的JavaWeb开发中,Spring生态圈占据着巨大的市场份额。几乎是每个互联网公司都在用Spring生态圈的东西。所以掌握Spring相关知识就成为了我们工作和面试中必不可......
  • npm 的配置文件 .npmrc
    一、.npmrc配置文件的作用.npmrc,可以理解成npmrunningcnfiguration,即npm运行时配置文件。简单点说,.npmrc可以设置package.json中依赖包的安装来源,既从哪里下载依......
  • TransactionScope的使用及遇到的坑
    事务的一般使用我们使用dotnet访问数据库,通常是这么写的(以PostgreSQL为例,其它数据库类似的)://其它数据库无非就是NpgsqlConnection换成别的,如SQLServer的SqlConnec......
  • 基于GenericAPIView+5个视图扩展类接口
    APIView与原生区别APIView属于drf视图基类#drf中APIView与django原生的View区别1.继承这个类后,视图方法中的request变为了drf的request而不是原生django的request2.......
  • Educational Codeforces Round 141:B. Matrix of Differences
    一、来源:Problem-B-Codeforces二、题面三、思路我们先从一维思考如何构造尽可能多的数值差。以n=2为例,此时有1,2,3,4数,其中构成差值为3的方案有一个1,4,构成差值......