简体   繁体   中英

how to send command to cmd with c#

I try to develop c# app to install apk to android phone with adb shell but I'm beginner for both. I cant send command to cmd.

   void getDevices()
    {
        Process p = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo(@"cmd.exe");

        p = Process.Start(startInfo);

        p.StandardInput.WriteLine(@"cd " + @"C:\Users\user\Desktop\adb\");
       // String output  = p.StandardOutput.ReadToEnd();





    }

There are three options:

  1. Pass the commands into cmd.exe when you start it
  2. Send the commands to cmd.exe after it has started
  3. Writing a batch file or PowerShell script

Pass the commands into cmd.exe when you start it

To pass commands to cmd.exe you can use the -k or -c parameters of cmd.exe .

For example, this will start cmd.exe , display the logged on user and then terminate the cmd.exe process:

cmd.exe /c "whoami"

Look at this page or Google for more examples.

Send the commands to cmd.exe after it has started

The alternative is to first start cmd.exe and later send commands to it. There are quite a few questions about that around already, but there generally seem two approaches:

  1. Send one key at a time, eg How can I send keypresses to a running process object?
  2. Send data to the input buffer of the process, eg .NET: Inject Data into Input-Buffer of Process

I'm not sure if they'll work with cmd.exe .

Writing a batch file or PowerShell script

The more common approach to automating cmd.exe is to write a batch file or PowerShell script. You may also want to consider these options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM