473,385 Members | 1,727 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,385 software developers and data experts.

How to interact a C# application with shell command?

Hi, wondering if there is a way to interact the shell command with the
C# program? For example, if I type
c:\>ver

it then suppose to return the version of the OS I am currently using...

or

c:\>systeminfo

then it will return a list of information of my currently system, and
what I want to do is to catch these information and use it in my c#
program?

Is there a way to do that?

Thank you all for the help.

Sincerely

Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
9 5015
Hi,

Actually there are classes to do that for you.
System.OperatingSystem will give you the version of the operating system.
I don't know what other system info you need.
Take a look at the System.Environment class and System.Diagnostics
namespace.

Greetings,

Bram.

"Tommy Lu" <lu****@email.msn.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi, wondering if there is a way to interact the shell command with the
C# program? For example, if I type
c:\>ver

it then suppose to return the version of the OS I am currently using...

or

c:\>systeminfo

then it will return a list of information of my currently system, and
what I want to do is to catch these information and use it in my c#
program?

Is there a way to do that?

Thank you all for the help.

Sincerely

Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Bram:

Thank you so much for your kind response.
I am trying to automate my test lab's scripts, and it involved heavily
on the shell scripting, more than just the OS information.
I am hoping to find the way to be able to execute the shell commend, and
the capture the result of that command.

And also, I found that System.Environment and Systeminformation class
are not working well under ASP.NET environment, can not get the correct
domain information, and wish to find a way to gather this info through
the shell command.

Thanks again for your time and resposne. :-)
Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Hi,

You could use Process.Start("application.exe") to launch the console
application or whatever application. That's the easiest part.
The only way, I think, to capture the output is to redirect it to a file
rather than the screen through piping. You do this by appending " >
output.txt" to the command line.

Just try it the "dir" command at the prompt.
C:\>dir > output.txt
You know have the output from the dir command in output.txt.

So, something like
Process.Start("C:\\SomePath\\YourConsoleApp.exe > C:\\output.txt");
should do the trick.

Greetings,

Bram.
"Tommy Lu" <lu****@email.msn.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
Bram:

Thank you so much for your kind response.
I am trying to automate my test lab's scripts, and it involved heavily
on the shell scripting, more than just the OS information.
I am hoping to find the way to be able to execute the shell commend, and
the capture the result of that command.

And also, I found that System.Environment and Systeminformation class
are not working well under ASP.NET environment, can not get the correct
domain information, and wish to find a way to gather this info through
the shell command.

Thanks again for your time and resposne. :-)
Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4
Hi Bram:
That's exactly what I had in mind, and I was hoping if there is any
build-in class / method provided by C# to save me time and troble to try
to parse through the re-directed file. I guess there is no other way to
get around it then.

Thank you so much for your time, and help responding to my problem.
Really appreciate your help! :-)
Tommy
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
You might look at the SetOut method of the Console class. You can use it to
redirect output from the console to a TextWriter, and from there you can do,
well, whatever you need to. There is an excellent example in the online
docs at (watch for wrap):
http://msdn.microsoft.com/library/de...etouttopic.asp

Cheers!
DL

"Tommy Lu" <lu****@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Bram:
That's exactly what I had in mind, and I was hoping if there is any
build-in class / method provided by C# to save me time and troble to try
to parse through the re-directed file. I guess there is no other way to
get around it then.

Thank you so much for your time, and help responding to my problem.
Really appreciate your help! :-)
Tommy
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #6
Here's an examplew I've been playing with.
I haven't got such an approach to work with
systeminfo, but this does work. I'm using v1.1.

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

myProcess.StartInfo.FileName = "ping";
myProcess.StartInfo.Arguments = "127.0.0.1";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;
myProcess.WaitForExit();

string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}

"Dave Loynd" <dl****@afts.com> wrote in message
news:u1*************@TK2MSFTNGP11.phx.gbl...
You might look at the SetOut method of the Console class. You can use it to redirect output from the console to a TextWriter, and from there you can do, well, whatever you need to. There is an excellent example in the online
docs at (watch for wrap):
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemconsoleclasssetouttopic.asp
Cheers!
DL

"Tommy Lu" <lu****@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Bram:
That's exactly what I had in mind, and I was hoping if there is any
build-in class / method provided by C# to save me time and troble to try
to parse through the re-directed file. I guess there is no other way to
get around it then.

Thank you so much for your time, and help responding to my problem.
Really appreciate your help! :-)
Tommy
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #7
And here is a second example I've been playing with.
Should have posted this with my first post.
This also doesn't seem to work with systeminfo.

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;

sIn.WriteLine("ping 127.0.0.1");
sIn.WriteLine("exit");
myProcess.WaitForExit();

string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
"Tommy Lu" <lu****@email.msn.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi, wondering if there is a way to interact the shell command with the
C# program? For example, if I type
c:\>ver

it then suppose to return the version of the OS I am currently using...

or

c:\>systeminfo

then it will return a list of information of my currently system, and
what I want to do is to catch these information and use it in my c#
program?

Is there a way to do that?

Thank you all for the help.

Sincerely

Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #8
Sorry to answer myself...
I've worked at little more on my previously posted examples.
Test before using. :-)

------------Example 1----------------
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "systeminfo";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;
string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
-----------end example1------------
-----------Example 2----------------
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;

sIn.WriteLine("systeminfo");
sIn.WriteLine("exit");
//myProcess.WaitForExit(); -- This hangs the program

string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
---------end example 2--------------

"Garrett" <ga********************@NOSPAMhotmail.com> wrote in message
news:Oh**************@TK2MSFTNGP11.phx.gbl...
And here is a second example I've been playing with.
Should have posted this with my first post.
This also doesn't seem to work with systeminfo.

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;

sIn.WriteLine("ping 127.0.0.1");
sIn.WriteLine("exit");
myProcess.WaitForExit();

string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
"Tommy Lu" <lu****@email.msn.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi, wondering if there is a way to interact the shell command with the
C# program? For example, if I type
c:\>ver

it then suppose to return the version of the OS I am currently using...

or

c:\>systeminfo

then it will return a list of information of my currently system, and
what I want to do is to catch these information and use it in my c#
program?

Is there a way to do that?

Thank you all for the help.

Sincerely

Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #9
I am seeing a problem when I put the Process routine in a seperate
"class library" project. Here is my code:

public void RunExportUtility (Credential c, string
parameterFileName)
{
// Start a new process to run the EXP utility
Process myApp = new Process();
myApp.StartInfo.WorkingDirectory="C:\\Documents and
Settings\\palessi\\My Documents\\Visual Studio
Projects\\Training\\TrainingAdmin\\bin\\Debug";
myApp.StartInfo.CreateNoWindow=false;
myApp.StartInfo.FileName = "C:\\oracle\\ora92\\bin\\EXP.EXE";
myApp.StartInfo.Arguments = c.Username + "/" + c.Password + "@" +
c.Database + " parfile=" + parameterFileName + ".par";
myApp.StartInfo.UseShellExecute = false;
myApp.StartInfo.RedirectStandardOutput=true;

myApp.Start();

Debug.WriteLine("TEST:" + myApp.StandardOutput.ReadToEnd());

myApp.WaitForExit();

/* The field OnExportComplete will either be null, if no client has
hooked up a delegate to the event
* , or else it refers to a delegate that should be called when the
event is invoked.
* Thus, invoking an event is generally done by first checking for
null and then calling the event.
*/
if (OnExportComplete != null)
// Raise the export complete event
OnExportComplete(this,new EventArgs());

}

All it does is take a credential and run the Oracle EXP utility. The
redirection goes to the output window in the IDE, but it isn't
prefixed with TEXT:. TEXT: is at the end of the output like so:

Export: Release 9.2.0.1.0 - Production on Tue Feb 10 13:55:04 2004

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 -
Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR
character set
server uses US7ASCII character set (possible charset conversion)
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported

About to export specified tables via Conventional Path ...
.. . exporting table ACFP_ACFT_TY 43 rows
exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
.. . exporting table ACFP_ACFT_TY_XREF 25 rows
exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
.. . exporting table ACFT 13587 rows
exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
.. . exporting table ACFT_ALPHA_CD_DOM 3 rows
exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
TEST:
The program '[2016] TrainingAdmin.exe' has exited with code 0 (0x0).

Any idea why this is happening? Basically my goal is to be able to
parse the output of EXP in real time and raise events to the calling
app which contain the name of the table being exported.

Any help would be greatly appreciated!

Pat
"Garrett" <ga********************@NOSPAMhotmail.com> wrote in message news:<#d**************@TK2MSFTNGP10.phx.gbl>...
Sorry to answer myself...
I've worked at little more on my previously posted examples.
Test before using. :-)

------------Example 1----------------
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "systeminfo";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;
string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
-----------end example1------------
-----------Example 2----------------
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;

sIn.WriteLine("systeminfo");
sIn.WriteLine("exit");
//myProcess.WaitForExit(); -- This hangs the program

string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
---------end example 2--------------

"Garrett" <ga********************@NOSPAMhotmail.com> wrote in message
news:Oh**************@TK2MSFTNGP11.phx.gbl...
And here is a second example I've been playing with.
Should have posted this with my first post.
This also doesn't seem to work with systeminfo.

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

class test{
static void Main()
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();

StreamWriter sIn = myProcess.StandardInput;
StreamReader sOut = myProcess.StandardOutput;
StreamReader sErr = myProcess.StandardError;

sIn.WriteLine("ping 127.0.0.1");
sIn.WriteLine("exit");
myProcess.WaitForExit();

string s = sOut.ReadToEnd();

Console.WriteLine(s);

sIn.Close();
sOut.Close();
myProcess.Close();
}
}
"Tommy Lu" <lu****@email.msn.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi, wondering if there is a way to interact the shell command with the
C# program? For example, if I type
c:\>ver

it then suppose to return the version of the OS I am currently using...

or

c:\>systeminfo

then it will return a list of information of my currently system, and
what I want to do is to catch these information and use it in my c#
program?

Is there a way to do that?

Thank you all for the help.

Sincerely

Tommy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Yann.K | last post by:
Hello. Using Tkinter, i would create a widget which display a shell command return. This return is long, and i would display a real time display (like with the tail -f commande on Linux) I...
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
1
by: Uwe Wagner | last post by:
Hi! I use the Shell command to open a second instance of Access. x = Shell(application & " " & Chr(34) & mdb & Chr(34) & " /user " & user & _ " /pwd " & password & " /wrkgrp " & Chr(34) &...
1
by: VRWC | last post by:
Hello dear people, In an A2K app, I have attempted to use the following command in some VBA code with IDENTICAL results with every single version of the following: Shell "outlook.exe", vbHide...
8
by: zhiwei wang | last post by:
I remember that there is a function that could invoke shell command such as "rm" "cp", directly in .c file. But I could not recall its name, and I googled with nothing meaningful. I vaguely...
2
by: jcrouse | last post by:
I apologize for starting another thread but the old one had a weird subject line. Anyways...here is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
2
by: ¹é¿ø¼® | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would not be run until a few minutes after running the...
1
by: ckirby | last post by:
I've inherited an application for creating mail merges from an Access 2003 ADP that needs an overhaul. To generate the mail merge , the app uses a shell command: result = Shell(str, vbHide) ...
5
by: billie | last post by:
Hi all. I would like to know if there's some python framework able to interact with system command prompt (cmd.exe or /bin/sh, depending on the system) from python. I need something that supports...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.