By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

How to execute commands in Command Line?

P: n/a
Hi All,
I want to run a command line appplication from C#. When i start
the application, it will go into specific mode. After that, i have to give
commands to use the application.
How Can This Be Done in C#....

Thanks and Regards,
S.Madhanmohan
Nov 15 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Madhanmohan S <er******@hotmail.com> wrote:
I want to run a command line appplication from C#. When i start
the application, it will go into specific mode. After that, i have to give
commands to use the application.
How Can This Be Done in C#....


Look at the System.Diagnostics.Process class and its Start method. To
write to the application's input, look at Process.StandardInput. If you
have any further questions, give a bit more detail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Hi,
I have application which will run in commandline mode. When i start
the application, it will go to a specific mode (Similar like, when we use
OSql for MSDE). In this mode, we have to give different commands to execute
our tasks. I tried with StandardInput Method. But i was not able to execute
the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.
Can you point some resources or sample code for the same???
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application.
How Can This Be Done in C#....


Look at the System.Diagnostics.Process class and its Start method. To
write to the application's input, look at Process.StandardInput. If you
have any further questions, give a bit more detail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3

P: n/a
Madhanmohan S <er******@hotmail.com> wrote:
I have application which will run in commandline mode. When i start
the application, it will go to a specific mode (Similar like, when we use
OSql for MSDE). In this mode, we have to give different commands to execute
our tasks. I tried with StandardInput Method. But i was not able to execute
the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.
Are you reading from StandardOutput and StandardError? Perhaps the
other process is blocking.
Can you point some resources or sample code for the same???


Sure. First, a very simple echo program:

using System;

public class Echo
{
static void Main()
{
string line;
while ((line=Console.ReadLine())!="quit")
Console.WriteLine ("Echo: {0}", line);
}
}

Compile it (to echo.exe, as a command-line app) and run it - basically
it echoes what you type until you type "quit".

Now here's something to automate it:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

public class AutoEcho
{
static void Main()
{
ProcessStartInfo psi = new ProcessStartInfo("echo.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);

// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();

proc.StandardInput.WriteLine ("Hello");
proc.StandardInput.WriteLine ("There");
proc.StandardInput.WriteLine ("quit");
}

class ProcessOutputReader
{
Process proc;

public ProcessOutputReader (Process proc)
{
this.proc = proc;
}

public void Start()
{
new Thread (new ThreadStart(ReadAll)).Start();
}

void ReadAll()
{
StreamReader reader = proc.StandardOutput;

string line;

while ((line = reader.ReadLine())!=null)
Console.WriteLine ("Process output: {0}", line);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

P: n/a
Thank You For Your Help.......
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
I have application which will run in commandline mode. When i start the application, it will go to a specific mode (Similar like, when we use OSql for MSDE). In this mode, we have to give different commands to execute our tasks. I tried with StandardInput Method. But i was not able to execute the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.


Are you reading from StandardOutput and StandardError? Perhaps the
other process is blocking.
Can you point some resources or sample code for the same???


Sure. First, a very simple echo program:

using System;

public class Echo
{
static void Main()
{
string line;
while ((line=Console.ReadLine())!="quit")
Console.WriteLine ("Echo: {0}", line);
}
}

Compile it (to echo.exe, as a command-line app) and run it - basically
it echoes what you type until you type "quit".

Now here's something to automate it:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

public class AutoEcho
{
static void Main()
{
ProcessStartInfo psi = new ProcessStartInfo("echo.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);

// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();

proc.StandardInput.WriteLine ("Hello");
proc.StandardInput.WriteLine ("There");
proc.StandardInput.WriteLine ("quit");
}

class ProcessOutputReader
{
Process proc;

public ProcessOutputReader (Process proc)
{
this.proc = proc;
}

public void Start()
{
new Thread (new ThreadStart(ReadAll)).Start();
}

void ReadAll()
{
StreamReader reader = proc.StandardOutput;

string line;

while ((line = reader.ReadLine())!=null)
Console.WriteLine ("Process output: {0}", line);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5

P: n/a
This is interesting.
But how do I know if an existing executable writes its output to
StandardOutput or StandardError?
I tried to read the output from the Lame MP3 encoder, and it didn't
work at first because it uses StandardError instead of StandardOutput.

- Magnus

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
I have application which will run in commandline mode. When i start the application, it will go to a specific mode (Similar like, when we use OSql for MSDE). In this mode, we have to give different commands to execute our tasks. I tried with StandardInput Method. But i was not able to execute the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.


Are you reading from StandardOutput and StandardError? Perhaps the
other process is blocking.
Can you point some resources or sample code for the same???


Sure. First, a very simple echo program:

using System;

public class Echo
{
static void Main()
{
string line;
while ((line=Console.ReadLine())!="quit")
Console.WriteLine ("Echo: {0}", line);
}
}

Compile it (to echo.exe, as a command-line app) and run it - basically
it echoes what you type until you type "quit".

Now here's something to automate it:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

public class AutoEcho
{
static void Main()
{
ProcessStartInfo psi = new ProcessStartInfo("echo.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);

// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();

proc.StandardInput.WriteLine ("Hello");
proc.StandardInput.WriteLine ("There");
proc.StandardInput.WriteLine ("quit");
}

class ProcessOutputReader
{
Process proc;

public ProcessOutputReader (Process proc)
{
this.proc = proc;
}

public void Start()
{
new Thread (new ThreadStart(ReadAll)).Start();
}

void ReadAll()
{
StreamReader reader = proc.StandardOutput;

string line;

while ((line = reader.ReadLine())!=null)
Console.WriteLine ("Process output: {0}", line);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6

P: n/a
Magnus Krisell <ma******@NOSPAMstudent.liu.se> wrote:
This is interesting.
But how do I know if an existing executable writes its output to
StandardOutput or StandardError?
Experimentation, basically.
I tried to read the output from the Lame MP3 encoder, and it didn't
work at first because it uses StandardError instead of StandardOutput.


You could always amalgamate the two - have two threads which each
listen to one of them and dump the data into a common place (eg an
ArrayList of the lines read), notifying another thread which reads the
data.

Alternatively, have two threads reading the different streams, but
doing the same thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

P: n/a
Hi Jon,
I still not able to execute commands in osql process.

I have attached the code which i am using. Please can you help me out?

---------------------------------Code
Starts --------------------------------------------------
ProcessStartInfo psi = new ProcessStartInfo(@"osql ","-Usa -PCHETTIAR");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.RedirectStandardError=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);
// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();
proc.StandardInput.WriteLine (@"sp_help GO");
proc.StandardInput.WriteLine();
try
{
//string atrError = proc.StandardError.ReadToEnd();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
proc.StandardInput.WriteLine ("mohan");
proc.StandardInput.WriteLine ("rahul");
}

-------------------------------Code
Ends -------------------------------------------------------

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
I have application which will run in commandline mode. When i start the application, it will go to a specific mode (Similar like, when we use OSql for MSDE). In this mode, we have to give different commands to execute our tasks. I tried with StandardInput Method. But i was not able to execute the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.


Are you reading from StandardOutput and StandardError? Perhaps the
other process is blocking.
Can you point some resources or sample code for the same???


Sure. First, a very simple echo program:

using System;

public class Echo
{
static void Main()
{
string line;
while ((line=Console.ReadLine())!="quit")
Console.WriteLine ("Echo: {0}", line);
}
}

Compile it (to echo.exe, as a command-line app) and run it - basically
it echoes what you type until you type "quit".

Now here's something to automate it:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

public class AutoEcho
{
static void Main()
{
ProcessStartInfo psi = new ProcessStartInfo("echo.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);

// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();

proc.StandardInput.WriteLine ("Hello");
proc.StandardInput.WriteLine ("There");
proc.StandardInput.WriteLine ("quit");
}

class ProcessOutputReader
{
Process proc;

public ProcessOutputReader (Process proc)
{
this.proc = proc;
}

public void Start()
{
new Thread (new ThreadStart(ReadAll)).Start();
}

void ReadAll()
{
StreamReader reader = proc.StandardOutput;

string line;

while ((line = reader.ReadLine())!=null)
Console.WriteLine ("Process output: {0}", line);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8

P: n/a
Madhanmohan S <er******@hotmail.com> wrote:
I still not able to execute commands in osql process.

I have attached the code which i am using. Please can you help me out? ---------------------------------Code
Starts --------------------------------------------------
ProcessStartInfo psi = new ProcessStartInfo(@"osql ","-Usa -PCHETTIAR");
First thing: there's nothing in the literal "osql" which suggests it
should be a verbatim literal. Just use "osql" without the @.
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.RedirectStandardError=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);
// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();
proc.StandardInput.WriteLine (@"sp_help GO");
proc.StandardInput.WriteLine();
try
{
//string atrError = proc.StandardError.ReadToEnd();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
proc.StandardInput.WriteLine ("mohan");
proc.StandardInput.WriteLine ("rahul");
}


Well, is the osql process even starting? You haven't specified where to
find it - is it on the path?

What happens when you try the code above? You've said it doesn't work,
but not what actually happens.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

P: n/a
Hi Jon,
osql is in system path. I am not able getting any reponse in the
reader. The code flows through smoothly with out any output or error.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
I still not able to execute commands in osql process.

I have attached the code which i am using. Please can you help me out?

---------------------------------Code
Starts --------------------------------------------------
ProcessStartInfo psi = new ProcessStartInfo(@"osql ","-Usa -PCHETTIAR");


First thing: there's nothing in the literal "osql" which suggests it
should be a verbatim literal. Just use "osql" without the @.
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.RedirectStandardError=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);
// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();
proc.StandardInput.WriteLine (@"sp_help GO");
proc.StandardInput.WriteLine();
try
{
//string atrError = proc.StandardError.ReadToEnd();
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
proc.StandardInput.WriteLine ("mohan");
proc.StandardInput.WriteLine ("rahul");
}


Well, is the osql process even starting? You haven't specified where to
find it - is it on the path?

What happens when you try the code above? You've said it doesn't work,
but not what actually happens.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10

P: n/a
Madhanmohan S <er******@hotmail.com> wrote:
osql is in system path. I am not able getting any reponse in the
reader. The code flows through smoothly with out any output or error.


So the program is definitely running, and the lines you're poking it
with are definitely being executed?

Note that using "ReadToEnd" is unlikely to work well as the stream
won't end until the process does... that may be the problem you're
having. I suggest you read the StandardError stream in the same way as
StandardOutput.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11

P: n/a

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
osql is in system path. I am not able getting any reponse in the
reader. The code flows through smoothly with out any output or error.
So the program is definitely running, and the lines you're poking it
with are definitely being executed?

True
Note that using "ReadToEnd" is unlikely to work well as the stream
won't end until the process does... that may be the problem you're
having. I suggest you read the StandardError stream in the same way as
StandardOutput. Already I have done the same way as you have suggested. when
I gave wrong user name or password, error was properly captured. But when i
was very much confussed why the sp_help is not getting executed............
Can you suggest any option for checking this or any other way of doing
it....
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12

P: n/a
Madhanmohan S <er******@hotmail.com> wrote:
So the program is definitely running, and the lines you're poking it
with are definitely being executed?

True

Note that using "ReadToEnd" is unlikely to work well as the stream
won't end until the process does... that may be the problem you're
having. I suggest you read the StandardError stream in the same way as
StandardOutput.

Already I have done the same way as you have suggested. when
I gave wrong user name or password, error was properly captured. But when i
was very much confussed why the sp_help is not getting executed............
Can you suggest any option for checking this or any other way of doing
it....


Sorry, I don't know. Of course, you could always tell osql to write to
a file instead, and capture the output afterwards. I don't know if
that's okay for you or not though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #13

P: n/a
Thank You For Your Help.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S <er******@hotmail.com> wrote:
So the program is definitely running, and the lines you're poking it
with are definitely being executed?

True

Note that using "ReadToEnd" is unlikely to work well as the stream
won't end until the process does... that may be the problem you're
having. I suggest you read the StandardError stream in the same way as
StandardOutput.

Already I have done the same way as you have suggested. when I gave wrong user name or password, error was properly captured. But when i was very much confussed why the sp_help is not getting executed............ Can you suggest any option for checking this or any other way of doing
it....


Sorry, I don't know. Of course, you could always tell osql to write to
a file instead, and capture the output afterwards. I don't know if
that's okay for you or not though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #14

P: n/a
"Madhanmohan S" <er******@hotmail.com> wrote in message news:<eb**************@TK2MSFTNGP09.phx.gbl>...
Hi All,
I want to run a command line appplication from C#. When i start
the application, it will go into specific mode. After that, i have to give
commands to use the application.
How Can This Be Done in C#....

Thanks and Regards,
S.Madhanmohan


Something like this should do the trick. Just fill in the places
where you want things to happen.

using System;

class myclass
{
public static void Main()
{
init();
string command = null;
while(true)
{
Console.Write("Prompt>");
command = Console.ReadLine();
dosomething(command);
}
}
private static void init()
{}
private static void dosomething(string input)
{}
}
Nov 15 '05 #15

P: n/a
"Madhanmohan S" <er******@hotmail.com> wrote in message news:<eb**************@TK2MSFTNGP09.phx.gbl>...
Hi All,
I want to run a command line appplication from C#. When i start
the application, it will go into specific mode. After that, i have to give
commands to use the application.
How Can This Be Done in C#....

Thanks and Regards,
S.Madhanmohan


Something like this should do the trick. Just fill in the places
where you want things to happen.

using System;

class myclass
{
public static void Main()
{
init();
string command = null;
while(true)
{
Console.Write("Prompt>");
command = Console.ReadLine();
dosomething(command);
}
}
private static void init()
{}
private static void dosomething(string input)
{}
}
Nov 15 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.