How to execute commands in Command Line?

  • Thread starter Thread starter Madhanmohan S
  • Start date Start date
M

Madhanmohan S

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
 
Madhanmohan S said:
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.
 
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???
 
Madhanmohan S said:
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);
}
}
}
 
Thank You For Your Help.......


Jon Skeet said:
Are you reading from StandardOutput and StandardError? Perhaps the
other process is blocking.


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);
}
}
}
 
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
 
Magnus Krisell said:
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.
 
Madhanmohan S said:
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.
 
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 -------------------------------------------------------
 
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.
 
Madhanmohan S said:
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.
 
Madhanmohan S said:
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 said:
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....
 
Thank You For Your Help.

Jon Skeet said:
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.
 
Madhanmohan S said:
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)
{}
}
 
Back
Top