Search code examples
c#buildnant

Start Nant from code


I am trying to start Nant using C# code. The same string entered in command line works fine but it won't work from code. Here is the code I am using

StringWriter consoleOut = new StringWriter( );
Console.SetOut( consoleOut );

ConsoleDriver.Main( new string[] { "-buildfile:" + filePath });

I am using nant.core to build the files externaly but it wont work... If I use the same command in cmd everything goes well... Starting it from code i get the following message

{NAnt 0.91 (Build 0.91.3881.0; alpha2; 17.08.2010.)\r\nCopyright ( C) 2001-2010 Gerry Shaw\r\nhttp://nant.sourceforge.net\r\n\r\n\r\nFor more information regarding the cause of the build failure, run the build again in debug mode.\r\n\r\nTry 'nant -help' for more information\r\n}

watching -help too but didnt find anything usefull :(


Solution

  • You can shell out to your executable that you would normally call from the command line:

                var startInfo = new ProcessStartInfo( executable, parameters )
                {
                    UseShellExecute = false,
                    RedirectStandardError = false,
                    RedirectStandardOutput = false,
                    CreateNoWindow = true
                };
    
                using( var process = Process.Start( startInfo ) )
                {
                    if( process != null )
                    {
                        process.WaitForExit( timeoutInMilliSeconds );
                        return process.ExitCode;
                    }
                }