Multiple commands on one line with Command Prompt and ADB - c#

I am trying to get an automated Android S-OFF method going in my program, but I can't seem to get two commands to follow each other in the same window. For example, my code right now is:
var process = Process.Start("CMD.exe", "/k adb shell chmod 744 /data/local/tmp/soffbin3");
process.WaitForExit();
However, when I run that, nothing occurs in the window. I tried to have the second command follow the first one like this:
var process = Process.Start("CMD.exe", "/k adb shell & chmod 744 /data/local/tmp/soffbin3");
process.WaitForExit();
However, the inclusion of & makes it so the second command doesn't go until the first command completes, and because "adb shell" isn't really a command that completes, it doesn't do the second command.
It was also suggested to me that I replace the /k with a /c, which may as well work, but the window closes almost instantly after opening that command, and I can't confirm that anything actually happened.
I've tried a few variations to try and get it to work, but nothing has worked so far. Is the answer something simple that I'm missing? I really hope it is.
Thanks in advance for the help!

This command work for me
var process = Process.Start("CMD.exe", #"/k adb pull data/data/com.sales.recorder/databases/SalesRecorder c:\adb");
process.WaitForExit();

You Can use the Verbatim escape for the command to avoid issues with your Literal commands.
The # is a verbatim escape so that the information following it in Double Quotes is run as a literal command.
var process = Process.Start("CMD.exe", #"/k adb shell & chmod 744 /data/local/tmp/soffbin3");
process.WaitForExit();

Related

Command line command won't run

I am trying to make a UI for some of the new Windows 10 Commands that have become useful to us. I am trying to make a UI that has three text boxes, one for the computer name, the username, and the message in order to utilize the msg command. When I run the command on my normal CMD, I would type msg /server:matthewl mlynch "hello",this will display a little message on my screen saying hello. However, when I try to run it from my c# ui, I get this
'msg' is not recognized as an internal or external command,
operable program or batch file.
As you can probably tell, this is not correct as it works when I launch command line and manually input but not when the UI runs the command.
Nevermind, coworker helped me, it was a setting in Project>Project Properties>Build>Platform Target and changed that to x64. Apparently it wasn't opening C:\Windows\System32, it was actually opening C:\Windows\Syswow64 but labeling itself as system32.
To run these commands, normally you'd use the System.Diagnostics.Process class to start the cmd.exe process, and then pass the command to the window:
Process.Start("cmd.exe",
$"/C msg /server:{txtServer.Text} {txtUser.Text} \"{txtMessage.Text}\"");

Advantages of using cmd.exe /c over not using cmd at all

If I'm running a process in C#. Is there any benefit to using cmd /c over just running the process directly?
Ex:
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c application.exe";
Process.Start(info);
as opposed to
ProcessStartInfo info = new ProcessStartInfo("application.exe");
Process.Start(info);
If you use cmd /c, you can execute multiple commands at once by separating the commands with &&. For example: cmd /c "application1.exe&&application2.exe". Otherwise if there is only one process to execute, there is no difference to simply executing the process directly.
EDIT: Apparently cmd /c can also return an error if the process does not exist, for example.
Another benefit of using CMD.EXE is if you want to execute a built-in command. Many commands in CMD.EXE are not separate executables but are wholly implemented within CMD.EXE such as dir, copy, mkdir, cd, and so forth. MKLINK is a built in command I've seen trip people up.
I don't know of a easy built in way to get a list of builtin commands. But you can get a list of CMD.EXE commands using HELP (which is not builtin!) and pass each command to WHERE. If not found by WHERE then the command must be builtin.
Also if you want to execute BAT/CMD files, CMD.EXE is the way to go.
The difference between cmd /c and cmd /k:
It is like saying cmd /?:
With cmd /c then executes commands series - the kernel ends.
With cmd /k then executes commands series - the kernel is on.
If you need that kernel is close, you must use cmd /c.
Otherwise only cmd /k.
Do not forget to get out of D.O.S. IO OS with the command: exit, exit, exit, until the windows is closed.

Different command prompt window behaviors

I've seen a very similar question asked before, here, however the answer thread fell apart. Basically, I installed Python (and checked the option to add to the Path variable), confirmed that it is indeed in the path variable (through the Environment Variables window like you would normally).
When opening a cmd window manually, I can type python -V and get the version back, and anything else really, and everything works fine, python is indeed exposed through the command prompt (when opened manually).
However, when I attempt to run a command through cmd.exe with a C# app I have, I get
'python' is not recognized as an internal or external command,
operable program or batch file.
The block of C# code I have
var proc = new Process();
var startInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "cmd.exe",
Arguments = "/K " + command
};
proc.StartInfo = startInfo;
proc.Start();
This has worked fine in the past. However I had my work machine upgraded to Windows 10 and have been struggling to get this working.
The command text hasn't changed since the application was working before my windows upgrade, and if I take it's text and run it through a manually opened command prompt it executes fine with no issues. So I'm hesitant to believe it's an issue with the command itself.
UPDATE: If I run echo %PATH% in a regular prompt I see python, if I run it in the command prompt my application opens, I do not. I tried using set PATH, but that didn't help. Why is it that the PATH variable is different between a command prompt I open manually and one my application opens?
I thought it might have something to do with User and System variables having their own path, but Python is in both of them when checked via System Properties, so I'm at a loss.

Non-Terminating Process Git Bash (C#)

As a fun little project, I am trying to use C# to operate the bash.exe provided by Git. I want the process to behave just as if I ran it in the Git Bash Application. By this, I mean I want to be able to execute command and get the output of said commands (i.e. if I enter the command "curl --version", I want to get the same output as the image here and be able to store it in a variable)
I have come very close to accomplishing this with the code here. However, with some commands, I find that the Process in C# never terminates. For example, if I try to execute the command "curl --help", I find the the Process never exits where "curl --version" did. As a quick hack, I figured out that I could fix this by changing the command to
curl --help >> output.txt
and then reading the .txt file. This does cause the command to exit and to write the correct output to the file, however, I don't like having to do this and I am sure there is a better solution to make commands of this sort exit properly. Thanks for the help!

How to execute a command from C#?

I am trying to execute a code from c# windows form application.
string cmdCode = "/C mogrify -crop 590x389+116+102! D:\\Backup\\Images\\*.TIF";
System.Diagnostics.Process.Start("CMD.exe", cmdCode);
But It doesnt do what it is supposed to do?
the code perfectly does the job when I type it in the command line. I already tried to change the path. I placed the tif files with MyApp.exe file and changed the cmdCode as
cmdCode = "/C mogrify -crop 590x389+116+102! *.TIF";
no success.. It shows up the black command promt very quickly and it gets disappeared.
I also tried to put the code in a file and make the extension .bat to run it but still no success!! any suggestions ?
Thanks
Try replacing the /C option with /K.
That will not solve your problem, but it should prevent the command prompt from disappearing, and allow you to see if any error is displayed by the prompt.
It's also possible that the PATH you are passing to CMD.exe is different from the one used by default, for some reason. Once you've started cmd.exe with the /K option, you should also be able to issue a echo %PATH% command, and see what you've got.
Remove the cmd.exe part and instead place there the path to mogrify.exe and start the parameters with "-crop...", removing the /C
If you're passing along path names that contain spaces you'll need to enclose the path with quotes, so the parameters will look like this:
"-crop \"590x389+116+102!\" \""+YourPathHere+"\"";

Categories