How to execute a command from C#? - 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+"\"";

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}\"");

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!

Execute command like Start>Run in C#

I want to create a command executor like Start > Run. The application has a TextBox and when a user enters a command eg : notepad "C:\test.txt" it should open notepad with this file or %PROGRAMFILES% it should open 'Programs Files' directory.
For %PROGRAMFILES% and other Windows variables I can use Environment.ExpandEnvironmentVariable and get their path and pass to Process.Start
and for notepad, I can split the with space and first part goes in FileName and rest of string goes in Arguments of ProcessStartInfo.
But what I want to know is, how does Start > Run work ? is there something like I can execute the whole command without splitting command-line or expanding the variables ? Maybe with pinvoke ?
To the very best of my knowledge, the run dialog is implemented with a call to ShellExecuteEx. You can achieve the same with Process.Start and UseShellExecute. You do need to expand the environment variables, and split the command into filename and arguments. You already describe how to do that in your question, and, again to the best of my knowledge, there is no programmatic interface to the functionality of the Run dialog.
However, what you can do programmatically is show the Run dialog. Create an instance of the Shell object requesting the IShellDispatch interface, and then call the FileRun method.

Multiple commands on one line with Command Prompt and ADB

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();

Calling .bat file from c#

Using this piece of code .exe running in server
string bat =null;
bat = "D:/folder/a.bat";
System.Diagnostics.Process.Start(bat);
Error: Could not find the specified file.
Can anyone help me on this.
Make sure the file really is located at that path.
Make sure your program has access to this path.
Use backslashes: bat = #"D:\folder\a.bat";
Filepath in Windows doesn't take a forward slash, it's not a URL/URI.
Use backslashes.
Anyone of below should work if the program has access to the bat file.
string bat=#"D:\folder\a.bat";
or
string bat="D:\\folder\\a.bat";
Also, checking for the existence of the bat file will be a good practice here:
if(File.Exists(bat))
{
System.Diagnostics.Process.Start(bat);
}
change the slashes to backslashes:
bat = "D:\\folder\\a.bat";
Typically you need to run an executable (like cmd.exe) and then pass it a parameter. cmd.exe specifically has two options /C (carries out the command specified by the string, then terminates) and /K (carries out the command specified by the string but remains open)
Wrong path. Try
bat = #"D:\folder\a.bat";

Categories