How to interact a C# application with shell command?

  • Thread starter Thread starter Tommy Lu
  • Start date Start date
T

Tommy Lu

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
 
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.
 
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
 
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.
 
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
 
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();
}
}
 
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();
}
}
 
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--------------
 
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
 
Back
Top