?
Solved

Shell command in c#

Posted on 2005-05-07
5
Medium Priority
?
301 Views
Last Modified: 2008-02-26
I am trying to use a shell command in c# to call a program which is installed on the server and I found an example in the Help files but it is throwing all sorts of errors, can someone give me some advise on how to do this, I previously did it in VB and it worked well.

thanks in Advance
CJSantora
0
Comment
Question by:CJSantora
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 13950689
you have to use Process.Start
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 13950691
0
 
LVL 8

Expert Comment

by:plq
ID: 13951502
I think its System.Diagnostics.Process.Start(...)

0
 
LVL 11

Accepted Solution

by:
jatinderalagh earned 2000 total points
ID: 13957005
Example of ping :
 
 Process myCmd = null;
               ProcessStartInfo qOptions = new ProcessStartInfo(@"cmd.exe", @"/C ping 127.0.0.1");
               qOptions.WindowStyle = ProcessWindowStyle.Hidden;

               qOptions.RedirectStandardOutput = true;  // set to "true" to enable logging
               qOptions.UseShellExecute = false;  // set to true to make silent/background
               
               myCmd = Process.Start(qOptions);

               StreamReader str = myCmd.StandardOutput;
               Console.WriteLine(str.ReadToEnd());
               
               myCmd.WaitForExit();


This essentially runs the ping command and returns the results to a string.  You will now have a string field with all of the contents that were returned.  You can do a simple query to determine if the results are a success or failure.

you can make it as background also
 qOptions.UseShellExecute = false;  // set to true to make silent/background


The line of code above... if you set it to true to make the process "silent" you won't be able to get a screen re-direct text.  

But then you need to redirect the output to some file or so.
0
 

Author Comment

by:CJSantora
ID: 13961766
Thank You for your help jatinderalagh
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question