0

I am new to c# programing.I want function like int system("command string") of c++ in c#.

for example: we can execute command on command prompt by using function system("winrm") in c++ where "winrm" is command. system() functon is in stdlib.h header file. I want to know whether this kind of function is available in c#. if yes let me know. Or can i use same system() method in c#? if yes,then tell me how to use? Thank you !!

sagar
  • 1,900
  • 5
  • 30
  • 45

3 Answers3

5

Seams that you are looking for Process Class. Info from MSDN:

Provides access to local and remote processes and enables you to start and stop local system processes.

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

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Added after comment.

The simplest way to execute command probably is static method Start(String) of Process class or the other one Start(String, String) that executes application with arguments.

Process.Start("application.exe");

or

Process.Start("application.exe", "param1 param2");

The above example gives you more flexibility. E.g. using ProcessStartInfo Class as a parameter to Start method that can redirect an input or output for console applications.

ryyker
  • 22,849
  • 3
  • 43
  • 87
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
  • Thank youit, but it is lengthy code for executing single command. i want only 1 line function which accepts command string as i mentioned in my question. – sagar Feb 29 '12 at 09:05
  • ok. if i want to execute 1000 command with diffrent parameter, then cmd window will open and close 1000 time. is it possible to hide cmd window while executing commands. i dont want to see what is output after executing command. – sagar Feb 29 '12 at 09:54
2

Basically you use the System.Diagnostics.Process class. You have several options:

Process.Start("something.exe", "/arguments"); // Just start a random .exe
Process.Start("cmd", "/C echo hi"); // Start a new command prompt, execute command, and automatically close command prompt at end
Process.Start("cmd", "/K echo hi"); // Start a new command prompt, execute command, but leave it open at the end.

Or you can use the full System.Diagnostics.ProcessStartInfo class to have a very fine control over how the new process is instantiated. You can wrap it in your own system() method if you need to reuse the same settings a lot.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

See the Process class

Shai
  • 25,159
  • 9
  • 44
  • 67