Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Angular build(ng build) from C#

Tags:

c#

angular

build

So I need to make a Angular build (ng build --prod --aot-false) for a C# project(ashx page) with the Angular folder as a part of the project.

What I have tried right now is make a bat file inside the Angular folder outside the "src" folder as follows:

test.bat:

mkdir a
ng build --prod --aot=false
mkdir b

When I execute the commands the directories "a" & "b" are created instantly but the build is never made.

To execute the process I use:

filehandler.ashx:

System.Environment.CurrentDirectory = 
context.Server.MapPath("~/ZipFiles/AngularProject_test/AngularProject/");
System.Diagnostics.Process.Start("test.bat");
like image 424
Mohit Agarwal Avatar asked Oct 17 '25 16:10

Mohit Agarwal


1 Answers

This thread is a bit old but maybe this will help someone else. The following snippet will run ng build from C#:

        // this will be the path to your app
        var workingDirectory = @"C:apps\angular-hello-world";

        // location of ng command 
        var command = @"C:\Users\Owner\AppData\Roaming\npm\ng.cmd";

        // any arguments you want to pass to ng
        var arguments = "build --prod --base-href /helloworld/ --output-path=prod";

        var process = new Process();
        var currentDirectory = Environment.CurrentDirectory;
        try
        {
            Environment.CurrentDirectory = workingDirectory;
            process.StartInfo.FileName = command;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WorkingDirectory = workingDirectory;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();

            // wait for the app to run and grab any output/errors generated
            process.WaitForExit();
            var output = process.StandardOutput.ReadToEnd();
            var errors = process.StandardError.ReadToEnd();
            Console.WriteLine("Output: " + output);
            Console.WriteLine("Errors: " + errors);
        }
        finally
        {
            Environment.CurrentDirectory = currentDirectory;
        }
like image 196
Jon Vote Avatar answered Oct 20 '25 06:10

Jon Vote