I was trying to execute a command through C#, but when I run the following code, a blank cmd window just opens up. The code:
string command = string.Format(#"adb install C:\Users\Mohit\Programming\Android_Workspace\{0}\bin\{0}.apk", appName);
ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
cmdsi.Arguments = command;
Process cmd = Process.Start(cmdsi);
What could be wrong? I am sure the syntax is right.
You need to add the /c argument before your command.
The /c argument tells the command
processor to open, run the specified
command, then close when it's done
string command = string.Format(#"/c adb install C:\Users\Mohit\Programming\Android_Workspace\{0}\bin\{0}.apk", appName);
ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
cmdsi.Arguments = command;
Process cmd = Process.Start(cmdsi);
For a complete list of arguments please refer to the documentation for cmd.
Related
Hello I have some issue with cmd command via c#
for example I do manually commands in cmd.exe
//command1 to pick directory
cd C:\Users\NewSystem\source\repos
//command2 command to send file to emulator
adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download
code I test in C# is and it doesnt work properly, exactly not send nothing to android emulator
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WorkingDirectory = #"C:\Users\NewSystem\source\repos\";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = false;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
To start cmd.exe and have it execute a command you need to pass /c (execute command and exit) or /k (execute command and remain)...
cmd /c adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download
You don't need to have cmd.exe start adb.exe, though; just start adb.exe directly.
As for your C# code, StandardInput is for writing to a process's stdin stream, which is not what you want. The way to pass command line arguments is using the ArgumentList (.NET (Core) 2.1+) or Arguments (all .NET Framework/Core versions) properties of ProcessStartInfo...
// Dispose cmd when finished
using (Process cmd = new Process())
{
// The full executable path is required if its directory is not in %PATH%
cmd.StartInfo.FileName = #"C:\Users\NewSystem\source\repos\adb.exe";
cmd.StartInfo.Arguments = #"-s emulator-5554 push ""Debug\funnels\01\01.mp4"" ""/sdcard/Download""";
cmd.StartInfo.WorkingDirectory = #"C:\Users\NewSystem\source\repos\";
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.WaitForExit();
}
I set Arguments to a verbatim string with the last two arguments quoted just to show how you would do so if either path contain spaces.
Similar question was asked at least a dozen times on SO and it looks that now I have exhausted most of the proposed solutions but still unable to complete the task successfully.
So what I have is the following command that I want to run in cmd:
xcopy /q C:\fileName.txt \\VMNAME\C$\destFolder /Y /E
But I need it to be executed with certain credentials. So what I was doing manually is entering the below command first:
runas /user:<domainName>\<userName> cmd
That was opening a separate cmd window and I was running the first (xcopy) command in that window.
What I have at the moment:
string strCmdText = string.Format(#"xcopy /q {0} {1} /Y /E", source, destination);
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.FileName = "runas";
procStartInfo.Arguments = String.Format(#"/user:<domainName>\<userName> cmd " + strCmdText);
Process.Start(procStartInfo);
Where the source and destination are of the below structure:
source = "C:\\somePath\\fileName.txt"
destination = "\\\\<VMName>\\C$\\somePath\\"
I have also tried defining procStartInfo with:
procStartInfo.Verb = "runas";
Instead of:
procStartInfo.FileName = "runas";
With similar results.
At the moment, when I run the above code, it does not return any error but doesn't do what's expected either. Am I missing something or this approach is wrong?
I am trying to create an app that starts cmd.exe and send command. It is important that the command is visible on cmd. Here is that I got so far but it doesn't seem to be working. Any idea?
Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever"); //I want this command to show up in cmd
When the code above is executed, run.exe is ran but the command does not show up in cmd.
What am I doing wrong?
Here's an addendum to my comment to make it clearer:
Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
//myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
System.IO.StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever");
This will allow everything outputted by cmd to show in the cmd console.
why you are using the streamwriter ?
you can use the Arguments
myProc.StartInfo.Arguments="run.exe --forever";
This is my code
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = #"cmd.exe";
proc.Arguments = "/C "+ "ipconfig" ;
System.Diagnostics.Process.Start(proc);
when I run this code , Cmd run and shut down so quickly .
How to pause it ?
THANKS A LOT :)
Specify the K parameter instead of C
From Microsoft documentation:
/c : Carries out the command specified by string and then stops.
/k : Carries out the command specified by string and continues.
proc.Arguments = "/K "+ "ipconfig" ;
more info: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true
Use /K instead of /C.
proc.Arguments = "/K " + "ipconfig";
You can see a list of command line switches here
/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables
Here is my code:
try
{
ProcessStartInfo procStartInfo = new ProcessStartInfo(
"cmd.exe",
"/c " + command);
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
procStartInfo.Verb = "runas";
procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command;
///command contains the command to be executed in cmd
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I want to keep
procStartInfo.UseShellExecute = true
procStartInfo.RedirectStandardInput = false;
Is it possible to execute the command without using process.standardinput?
I try to execute command I've passed in argument but the command does not executes.
As #mtijn said you've got a lot going on that you're also overriding later. You also need to make sure that you're escaping things correctly.
Let's say that you want to run the following command elevated:
dir c:\
First, if you just ran this command through Process.Start() a window would pop open and close right away because there's nothing to keep the window open. It processes the command and exits. To keep the window open we can wrap the command in separate command window and use the /K switch to keep it running:
cmd /K "dir c:\"
To run that command elevated we can use runas.exe just as you were except that we need to escape things a little more. Per the help docs (runas /?) any quotes in the command that we pass to runas need to be escaped with a backslash. Unfortunately doing that with the above command gives us a double backslash that confused the cmd parser so that needs to be escaped, too. So the above command will end up being:
cmd /K \"dir c:\\\"
Finally, using the syntax that you provided we can wrap everything up into a runas command and enclose our above command in a further set of quotes:
runas /env /user:Administrator "cmd /K \"dir c:\\\""
Run the above command from a command prompt to make sure that its working as expected.
Given all that the final code becomes easier to assemble:
//Assuming that we want to run the following command:
//dir c:\
//The command that we want to run
string subCommand = #"dir";
//The arguments to the command that we want to run
string subCommandArgs = #"c:\";
//I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
//Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
string subCommandFinal = #"cmd /K \""" + subCommand.Replace(#"\", #"\\") + " " + subCommandArgs.Replace(#"\", #"\\") + #"\""";
//Run the runas command directly
ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
//Create our arguments
string finalArgs = #"/env /user:Administrator """ + subCommandFinal + #"""";
procStartInfo.Arguments = finalArgs;
//command contains the command to be executed in cmd
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
}
why are you initializing the process object with arguments and then later on override those Arguments? and btw: the last bit where you set Arguments you concatenate 'command' right upto 'cmd', that doesn't make much sense and might be where it fails (looks like you're missing a space).
Also, you are currently using the standard command line, you might want to look into using the runas tool instead. you can also call runas from command line.
Also, why are you running 'command' from the command line? why not start it directly from Process.Start with admin privileges supplied then and there? here's a bit of pseudocode:
Process p = Process.Start(new ProcessStartInfo()
{
FileName = <your executable>,
Arguments = <any arguments>,
UserName = "Administrator",
Password = <password>,
UseShellExecute = false,
WorkingDirectory = <directory of your executable>
});