Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Running MSBuild Commands from My C# console application.

Running MSBuild Commands from My C# console application.

Scheduled Pinned Locked Moved C#
csharpvisual-studiocollaborationquestionworkspace
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Madhurima Dutta
    wrote on last edited by
    #1

    i have been using cliwrap for running git commands and passing the arguments. But it seems to have failed while running my msbuild command from my c# console application. What i am trying- var stdOutSetup = new StringBuilder(); var stdErrSetup = new StringBuilder(); var Setup = await Cli.Wrap("msbuild") .WithArguments("/t:scmclean && /t:setup") .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutSetup)) .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrSetup)) .ExecuteBufferedAsync(); var stdOut2 = stdOutSetup.ToString(); var stdErr2 = stdErrSetup.ToString(); Console.WriteLine("Build Commands Output :"); Console.WriteLine(stdOut2); Console.WriteLine(stdErr2); is there any possible way where i can run msbuild commands from my c# console application?

    D J 2 Replies Last reply
    0
    • M Madhurima Dutta

      i have been using cliwrap for running git commands and passing the arguments. But it seems to have failed while running my msbuild command from my c# console application. What i am trying- var stdOutSetup = new StringBuilder(); var stdErrSetup = new StringBuilder(); var Setup = await Cli.Wrap("msbuild") .WithArguments("/t:scmclean && /t:setup") .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutSetup)) .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrSetup)) .ExecuteBufferedAsync(); var stdOut2 = stdOutSetup.ToString(); var stdErr2 = stdErrSetup.ToString(); Console.WriteLine("Build Commands Output :"); Console.WriteLine(stdOut2); Console.WriteLine(stdErr2); is there any possible way where i can run msbuild commands from my c# console application?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Yes, you just have to supply the correct command line to run MsBuild. You don't say what the error is that you're getting back, and that's really important when troubleshooting problems. My guess is going to be you're not supplying the path to MsBuild and/or you're not supplying any required command line parameters for it to know what you want to do. AND STOPP SPAMMING THE SITE WITH THE SAME QUESTION OVER AND OVER AGAIN!

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      1 Reply Last reply
      0
      • M Madhurima Dutta

        i have been using cliwrap for running git commands and passing the arguments. But it seems to have failed while running my msbuild command from my c# console application. What i am trying- var stdOutSetup = new StringBuilder(); var stdErrSetup = new StringBuilder(); var Setup = await Cli.Wrap("msbuild") .WithArguments("/t:scmclean && /t:setup") .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutSetup)) .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrSetup)) .ExecuteBufferedAsync(); var stdOut2 = stdOutSetup.ToString(); var stdErr2 = stdErrSetup.ToString(); Console.WriteLine("Build Commands Output :"); Console.WriteLine(stdOut2); Console.WriteLine(stdErr2); is there any possible way where i can run msbuild commands from my c# console application?

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Open a console window that represents the OS shell (console) that your command is supposed to be running in. Open in the root directory. Then type the following. It will fail I am sure. It will not work in your program until you can get it to run from the shell.

        msbuild /t:scmclean && /t:setup

        I did not investigate it too much but I am rather certain that however you are doing it (which shell) that that '&&' is wrong.

        M 1 Reply Last reply
        0
        • J jschell

          Open a console window that represents the OS shell (console) that your command is supposed to be running in. Open in the root directory. Then type the following. It will fail I am sure. It will not work in your program until you can get it to run from the shell.

          msbuild /t:scmclean && /t:setup

          I did not investigate it too much but I am rather certain that however you are doing it (which shell) that that '&&' is wrong.

          M Offline
          M Offline
          Madhurima Dutta
          wrote on last edited by
          #4

          I have tried running msbuild /t:scmclean from my command prompt, it works fine , but when i try to call the same command from my c# console application using the code below namespace BuildCode { class BuildCM { public static void Main() { Process msetup = new Process(); msetup.StartInfo.FileName = "cmd.exe"; msetup.StartInfo.CreateNoWindow = true; msetup.StartInfo.RedirectStandardInput = true; msetup.StartInfo.RedirectStandardOutput = true; msetup.StartInfo.UseShellExecute = false; msetup.Start(); msetup.StandardInput.WriteLine("msbuild.exe D:\\git\\btf_pi_testrack\\Build\\Tools /t:scmclean"); msetup.StandardInput.Flush(); msetup.StandardInput.Close(); msetup.WaitForExit(); Console.WriteLine(msetup.StandardOutput.ReadToEnd()); Console.ReadKey(); } } } i get an error: C:\Users\JediAdmin\source\repos\msbuild\bin\Debug\net7.0>msbuild.exe D:\git\btf_pi_testrack\Build\Tools /t:scmclean MSBuild version 17.4.0+18d5aef85 for .NET MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file., it fails to run msbuild command for the particular directory but if i execute msbuild.exe D:\git\btf_pi_testrack\Build\Tools /t:scmclean in command prompt, it works fine. Also i did a little bit of digging for this issue and came back with Microsoft.Build.Runtime and Microsoft.Build.utilities.core for running msbuild commands from console application, however i couldn't get any code example to understabd how it does that? any idea on this as well?

          J 1 Reply Last reply
          0
          • M Madhurima Dutta

            I have tried running msbuild /t:scmclean from my command prompt, it works fine , but when i try to call the same command from my c# console application using the code below namespace BuildCode { class BuildCM { public static void Main() { Process msetup = new Process(); msetup.StartInfo.FileName = "cmd.exe"; msetup.StartInfo.CreateNoWindow = true; msetup.StartInfo.RedirectStandardInput = true; msetup.StartInfo.RedirectStandardOutput = true; msetup.StartInfo.UseShellExecute = false; msetup.Start(); msetup.StandardInput.WriteLine("msbuild.exe D:\\git\\btf_pi_testrack\\Build\\Tools /t:scmclean"); msetup.StandardInput.Flush(); msetup.StandardInput.Close(); msetup.WaitForExit(); Console.WriteLine(msetup.StandardOutput.ReadToEnd()); Console.ReadKey(); } } } i get an error: C:\Users\JediAdmin\source\repos\msbuild\bin\Debug\net7.0>msbuild.exe D:\git\btf_pi_testrack\Build\Tools /t:scmclean MSBuild version 17.4.0+18d5aef85 for .NET MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file., it fails to run msbuild command for the particular directory but if i execute msbuild.exe D:\git\btf_pi_testrack\Build\Tools /t:scmclean in command prompt, it works fine. Also i did a little bit of digging for this issue and came back with Microsoft.Build.Runtime and Microsoft.Build.utilities.core for running msbuild commands from console application, however i couldn't get any code example to understabd how it does that? any idea on this as well?

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Madhurima Dutta wrote:

            msbuild /t:scmclean

            First I doubt it ran find from the root directory. Second that is not the command that you are attempting to run in the code you originally posted. Third what you posted here does not match what msbuild documentation expects. MSBuild Command-Line Reference - MSBuild | Microsoft Learn[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups