Problem with try catch to show error message.

ken76

Member
Joined
Nov 15, 2018
Messages
23
Programming Experience
5-10
I don't get any error message if type a faulty computer name with the code below this text.
Can someone help me, how I can get it done?

C#:
 try
             {
                 buttonStatus.Enabled = false;
                 statusComputer = textBoxComputer.Text;
                 ProcessStartInfo status = new ProcessStartInfo("cmd");
                 status.Arguments = "/c systeminfo /s " + statusComputer + " | findstr /B " + winVersion + " " + osName + " " + systemInfo + " " + installDate;
                
    
                 //mesessageBox.Show(checkComputer);
                 status.UseShellExecute = false;
                 status.CreateNoWindow = true;
                 status.WindowStyle = ProcessWindowStyle.Hidden;
                 status.RedirectStandardOutput = true;
                 status.RedirectStandardInput = false;
    
                 var prog = Process.Start(status);
    
                 string statustext = prog.StandardOutput.ReadToEnd();
                 richTextBoxStatus.Text = statustext;
                 MessageBox.Show("Task completed!");
             }
             catch
             {
                 MessageBox.Show("Error!");
             }
             buttonStatus.Enabled = true;
 
Why would you expect to get an exception? Cmd.exe (almost always) will launch without any problems.

An exception will be thrown if the process could not be started. CMD.exe usually succeeds. So what if the command you are passing on to cmd.exe fails. That is a separate failure that you'll need to capture a different way.
 
Redirect StandardError and read the error message there.
 
As an aside, it looks like all the information you are trying to retrieve from 'systeminfo' is information that you could retrieve by using WMI directly. C# lets you access WMI.

 
Back
Top Bottom