Good day. There is a file written in C #, for example 'app.cs'. You need to compile this file through an application in C #. If the compiler produces a compile error, then you must redirect the output to a text file. The problem is that from C # a PowerShell script is executed in which the compiler itself is launched. I do not quite understand PowerShell, so I appeal to the community. The following is the code:

  • PowerShell Script Code

@ "Start-Process -FilePath 'C: \ Program Files \ MSBuild \ 12.0 \ Bin \ csc.exe' -ArgumentList 'app.cs' -Wait -NoNewWindow | Out-File -FilePath log.txt"

  • C # code

    string script = ... // см. выше Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(script); Collection<PSObject> result = pipeline.Invoke(); runspace.Close(); 

ps The result variable is also empty.

  • here I already answered similar for CMD. In your case "| Out-File-FilePath log.txt" - redirection to a file using PowerShell, try to adapt the solution from that answer, if you do not understand it yourself, I will write here separately for PowerShell - rdorn
  • msdn.microsoft.com/ru-ru/library/78f4aasd.aspx is the starting point for learning compilation from the command line - rdorn

1 answer 1

Thanks @rdorn. It was necessary to override the output. PowerShell response

 $objectStartInfo = New-Object System.Diagnostics.ProcessStartInfo -Property @{ "FileName" = "C:\Program Files\MSBuild\12.0\Bin\csc.exe" "Arguments" = "p1.cs" "UseShellExecute" = $false "RedirectStandardOutput" = $true } $processObject = New-Object System.Diagnostics.Process $processObject.StartInfo = $objectStartInfo Register-ObjectEvent -InputObject $processObject -EventName OutputDataReceived -action{ $Event.SourceEventArgs.Data >> log.txt } $processObject.Start() $processObject.BeginOutputReadLine() 
  • Well, you got a very straight C # -way. The PowerShell way would look something like this: Start-Process -FilePath 'csc.exe' -ArgumentList 'app.cs' -RedirectStandardOutput 'output.log' -RedirectStandardError 'error.log' . - n01d
  • @ n01d, I later rewrote the compiler through API - Vadim Prokopchuk