-3

First of all, i searched a lot to avoid asking a duplicate question. If there is one, i will delete this question immediately.

All the solutions on the web are suggesting to use Process.StartInfo like this one

How To: Execute command line in C#, get STD OUT results

I dont want to run a batch file, or an .exe.

I just want to run some commands on cmd like

msg /server:192.168.2.1 console "foo" or ping 192.168.2.1

and return the result if there is one.

How can i do that ?

Community
  • 1
  • 1
Emre Acar
  • 920
  • 9
  • 24
  • Probably duplicate of [link](http://stackoverflow.com/a/1469790/1617002) – Fka Nov 13 '14 at 11:30
  • possible duplicate of [Run Command Prompt Commands](http://stackoverflow.com/questions/1469764/run-command-prompt-commands) – DIF Nov 13 '14 at 11:35

3 Answers3

0

Those commands are still exe files, you just need to know where they are. For example:

c:\windows\system32\msg.exe /server:192.168.2.1 console "foo"
c:\windows\system32\ping.exe 192.168.2.1
DavidG
  • 113,891
  • 12
  • 217
  • 223
0

The only proper way to do this is to use Process.Start. This is demonstrated adequately in this question, which is itself a duplicate of two others.

However, as DavidG says, the commands are all exe files and you can run them as such.

Community
  • 1
  • 1
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
-1

Apparently, i found an answer

while (true)
            {
                Console.WriteLine("Komut giriniz.");
                string komut = Console.ReadLine();
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = "cmd.exe";
                startInfo.Arguments = "/C" + komut;
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;
                process.StartInfo = startInfo;
                Console.WriteLine(process.Start());
                string line = "";
                while (!process.StandardOutput.EndOfStream)
                {
                    line = line + System.Environment.NewLine + process.StandardOutput.ReadLine();
                    // do something with line
                }
                Console.WriteLine(line);
                Console.ReadLine();
            }

seems like if you can run cmd.exe with arguments including your command.

thanks for contributing.

Emre Acar
  • 920
  • 9
  • 24
  • If this is what you wanted to achieve, you should probably have specified in your question that you wanted your application to accept cmd commands from stdin. – PaddyD Nov 13 '14 at 11:38