473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to execute commands in Command Line?

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
15 14024
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
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
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
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
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
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
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
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
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
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

"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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I used to execute a command line script in IIS 4 using Dynuexec. This is a third part server object I installed on the server (http://www.dynu.com/dynuexec.asp). I installed this object onto my...
1
by: ktsirig | last post by:
Hi all, I need to execute a command-line program through PHP. I have a form which includes a textarea, where the user writes his data. These data will be used as input for the command-line...
2
by: Adam Clauss | last post by:
I am building a GUI to wrap around some of the information/abilities contained within the program netsh. I have figured out how to use redirect the standardinput and standardoutput so that I can...
6
by: Lucas Cowald | last post by:
Hi, I need to RUN/execute a Command-Line command from an ASP page. This is the command: sse45.exe -i k:\o\2.wmv -o k:\o\2.shh -w 128 -df 0 -m 2 -p Can you show me a code how to run this...
34
by: Roman Mashak | last post by:
Hello, All! I'm implementing simple CLI (flat model, no tree-style menu etc.). Command line looks like this: <command> <param1> <param2> ... <paramN> (where N=1..4) And idea is pretty simple: ...
1
by: stikhs | last post by:
Hi! I want to execute a command line application through my programme!Is there any instruction that could help me do this?I want to give the arguments via the GUI that I build,so is it possible...
0
by: drken567 | last post by:
I'm trying to execute a command line from my C++ .NET code/WinXP. The program (call it myprog.exe) is located in a directory (C:\mypath) which is included in the system 'path' environment variable....
15
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands...
7
by: swethak | last post by:
Hi, i have a command to convert the video file into image ffmpeg -i sample.wmv -f image2 -t 0.001 -ss 3 ss.jpg i run that one in command prompt it converted the video file into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.