Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I want to open cmd and send "systeminfo" to it in order to get some system information,
but this code doesn't work :

C#
private void Form1_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd";
            p.StartInfo.Arguments = "systeminfo";
            p.Start();
        }

what should I do?
Thanks.
Posted

Instead of starting a new cmd, try simply starting the msinfo32.exe which will launch the System Information window. And if you need command line arguments, have a look at this document: http://support.microsoft.com/kb/300887[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 15:27pm    
This is correct, my 5. Systeminfo.exe will also do it.
--SA
Wendelius 5-Apr-11 15:52pm    
Thanks :)
mehdi_k 5-Apr-11 16:11pm    
thank's but my exact problem is how to execute a command in cmd by using c#, system info was an example.
Wendelius 5-Apr-11 16:21pm    
Ok, sorry about the misunderstanding. So if you want to use CMD to run something, perhaps you're looking for this kind of example:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "cmd";
psi.Arguments = "/K \"DIR C:\"";
process.StartInfo = psi;
process.Start();
I used /K switch so that the window will remain open in order to see the behaviour.
mehdi_k 6-Apr-11 7:02am    
It works , thanks
do you know how can I execute more than 1 command in CMD?
for example :
first it change it's direction with "cd d:\"
and then some another commands.
For the example you provided, just set the FileName property to systeminfo.exe. You should also research the Process class a bit more. You probably need other properties to be set before calling Process.Start().
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 15:28pm    
Exactly, my 5.
--SA
All your code does is launch the command. It doesn't log the information returned anywhere and your code doesn't read StdOut of the process you launched.

On top of that, all of the information you get from SystemInfo can be had with a proper application of WMI queries. You don't need to read and parse any external command at all.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 15:28pm    
True. My 5.
--SA
You could try this link How to Execute a Command in C# ?[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900