简体   繁体   中英

Check if process indirectly resulted in an error

I have the following code that I'm using to open cmd and run the SQL Server setup.exe with a configuration file.

            ProcessStartInfo pStartInfo = new ProcessStartInfo();
            pStartInfo.FileName = "cmd.exe";
            pStartInfo.Arguments = "/c /q setup.exe /c /q /configurationFile=../configurationFile.ini";
            pStartInfo.WorkingDirectory = installDir + @"\SQL Server Unpacked";
            pStartInfo.CreateNoWindow = true;
            pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            pStartInfo.UseShellExecute = false;
            pStartInfo.RedirectStandardInput = true;
            pStartInfo.RedirectStandardOutput = true;
            pStartInfo.RedirectStandardError = true;

            Process p = new Process();
            p.StartInfo = pStartInfo;

            lb_SQLServerReadout.Text = "Please wait while the setup runs";
            p.Start();

This is working alright. It'll attempt to install with the details of the configuration file, and if the configuration file contains invalid details then the setup.exe will give an error and not install.

I want to be able to get that error code and write it to a label on my form, so the user has some way of knowing that an error occurred and why it occurred. I considered using Process.StandardError.ReadToEnd , but it will always return null . I believe this is because the Process itself is cmd, not setup.exe (where the error actually occurs).

I've read that it's possible to get the last error code in cmd using echo %errorlabel% , and that does work when I run it manually, so I tried implementing it in my code:

            p.StandardInput.WriteLine("echo %errorlevel%");
            string s = p.StandardOutput.ReadLine();
            p.WaitForExit();

I was hoping that s would be set to the output of the echo %errorlevel% input, but it is just set to null .

How can I get the error code of SQL Server setup, through my cmd Process ?

Just as a side note, I've considered running setup.exe as the Process , but it requires elevation, which means enabling UseShellExecute , which in turn means disabling RedirectStandardError , which would prevent me getting the error code anyway.

That is all not necessary, the exit code of the "executable" invoked with cmd.exe /c will be the exit code of cmd.exe itself.

You can try this on the command line as well:

c:\> cmd.exe /c app.exe make-it-fail
c:\> echo %errorlevel%

The second line will print the exit code of "app.exe make-it-fail".

So, just read the value of the Process.ExitCode property, after Process.WaitForExit() .

What particular values setup.exe (of SQL Server) sets in case it fails, I don't know. Should be something different than 0 though (by convention).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM