tags:

views:

654

answers:

5

I have a set of commands like:

C:
cd Project
testproj.exe

My system gets these commands one by one from a remote system. I need to execute each command in cmd.exe on receiving the command from the remote system. How to execute these using .NET?

I also need to return the result of testproj.exe to the remote machine. How to take the result after running command?

+4  A: 

Take a look at System.Diagnostics.Process. You can redirect stdout/stderr somewhere to get the output.

elan
+3  A: 
var process = System.Diagnostics.Process.Start( "testproj.exe" );
process.WaitForExit();
var result = process.ExitCode;

This won't really honor things like "C:" or "CD path". Instead you'd want to create a batch file in a temporary folder then call the batch file.

Paul Alexander
This won't work. You need a process.WaitForExit() in order to get the ExitCode.
Adam Robinson
+1  A: 

The C: and cd Project operations can be done inside the lanching application using the Directory class using the SetCurrentDirectory method.

Then just use the Process class to launch the testproj.exe executable.

jussij
+1  A: 

Instead of trying to support all the commands of DOS, just have a small subset implemented which will guarantee nothing can go wrong. Like Don't allow DELETE, RD, FORMAT etc.

So, basically you would only have a subset of DOS commands. Once you have the command set, you can code for those specific commands using a extension mechanisms or as pluggable modules.

This will also helps you safe guard your machine from malicious attacks and worst to happen is there could be data sent out but from machine, the data / system can never be harmed.

UPDATE: The implementaion of specific commands is left to you. You could use .NET API or have System.Diagnostics.Process

Ramesh
+4  A: 

Process.Start cmd.exe, and hook StandardIn, StandardOut, and StandardError. Then, when a command comes in, just write it to StandardIn and read StandardOut/Error for the return. The whole thing shouldn't be more than 15 LOC.

That being said, just installing the Telnet Server would probably be easier - as it sounds like that's what you're essentially replicating....

Mark Brackett
it worked! thanks a lot!
Mahatma