-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 don't 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 ?

2
  • Probably duplicate of link Commented Nov 13, 2014 at 11:30
  • possible duplicate of Run Command Prompt Commands Commented Nov 13, 2014 at 11:35

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.