Problem with try catch to show error message.

ken76

Member
Joined
Nov 15, 2018
Messages
9
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;
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,122
Location
Chesapeake, VA
Programming Experience
10+
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.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,183
Location
Norway
Programming Experience
10+
Redirect StandardError and read the error message there.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,122
Location
Chesapeake, VA
Programming Experience
10+
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.

 
Top Bottom