I have created a Process to run command in CMD.
var process = Process.Start("CMD.exe", "/c apktool d app.apk");
process.WaitForExit();
How can I run this command without displaying actual CMD window?
You can use the WindowsStyle-Property to indicate whether the process is started in a window that is maximized, minimized, normal (neither maximized nor minimized), or not visible
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Source:
Property:MSDN
Enumartion: MSDN
And change your code to this, becaeuse you started the process when initializing the object, so the properties (who got set after starting the process) won't be recognized.
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c apktool d app.apk";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
There are several issues with your program, as pointed out in the various comments and answers. I tried to address all of them here.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "apktool";
//join the arguments with a space, this allows you to set "app.apk" to a variable
psi.Arguments = String.Join(" ", "d", "app.apk");
//leave it to the application, not the OS to launch the file
psi.UseShellExecute = false;
//choose to not create a window
psi.CreateNoWindow = true;
//set the window's style to 'hidden'
psi.WindowStyle = ProcessWindowStyle.Hidden;
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
The main issues:
using cmd /c when not necessary
starting the app without setting the properties for hiding it
Try this :
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.WaitForExit();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
Related
I got 1 mapper (.exe) and 1 driver (.sys) and i want to do so that when i execute them i want to run mapper as admin with the driver and not create a window (keep it hidden). can anyone help. I have the following code down but nor does it run the mapper as admin with the spoof and it still also creates a window! help!
string map = "C:\\SCSpoofer\\mapper.exe";
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess = Process.Start(map, sys);
System.Threading.Thread.Sleep(150);
myProcess.Kill();```
// Create a Process to launch a command window (hidden) to create the item templates
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = Project.ServicesFolder;
startInfo.Arguments = "/C " + CreateServices;
process.StartInfo = startInfo;
process.Start();
This is from my open source project DataTier.Net github.com/DataJuggler/DataTier.Net
I have a few commands I want to run through the CMD (admin):
ipconfig/flushdns
ipconfig /registerdns
ipconfig /release
ipconfig /renew
netsh winsock reset
Anyone can tell me how?
Question of is sort answered here: Running cmd commands with Administrator rights
The code could be duplicated (as shown below):
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ipconfig /flushdns";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo2 = new
System.Diagnostics.ProcessStartInfo();
startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo2.FileName = "cmd.exe";
startInfo2.Arguments = "/C ipconfig /renew";
startInfo2.Verb = "runas";
process2.StartInfo = startInfo2;
process2.Start();
If you are looking to use powershell, you can create a script which will include your commands something like this:
& "command 1";
& "command 2";
etc.
I currently have a portion of code that creates a new Process and executes it from the shell.
Process p = new Process();
...
p.Start();
p.WaitForExit();
This keeps the window open while the process is running, which is great. However, I also want to keep the window open after it finishes to view potential messages. Is there a way to do this?
It is easier to just capture the output from both the StandardOutput and the StandardError, store each output in a StringBuilder and use that result when the process is finished.
var sb = new StringBuilder();
Process p = new Process();
// redirect the output
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// hookup the eventhandlers to capture the data that is received
p.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
p.ErrorDataReceived += (sender, args) => sb.AppendLine(args.Data);
// direct start
p.StartInfo.UseShellExecute=false;
p.Start();
// start our event pumps
p.BeginOutputReadLine();
p.BeginErrorReadLine();
// until we are done
p.WaitForExit();
// do whatever you need with the content of sb.ToString();
You can add extra formatting in the sb.AppendLine statement to distinguish between standard and error output, like so: sb.AppendLine("ERR: {0}", args.Data);
This will open the shell, start your executable and keep the shell window open when the process ends
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
p.StartInfo = psi;
p.Start();
p.WaitForExit();
or simply
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
if(p != null && !p.HasExited)
p.WaitForExit();
Be carefull espacially on switch /k, because in many examples is usually used /c.
CMD /K Run Command and then return to the CMD prompt.
CMD /C Run Command and then terminate
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k yourmainprocess.exe";
p.Start();
p.WaitForExit();
Regarding: "Member Process.Start(ProcessStartInfo) cannot be accessed with an instance reference; qualify it with a type name instead"
This fixed the problem for me....
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
p.WaitForExit();
I have a problem, I start cygwin commands with c#, what is working fine. The Problem is to set the working directory.
pathANDcommand = C:\cygwin\bin\ls.exe";
arg = " -lisa";
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = pathANDcommand;
startInfo.Arguments = arg;
output.Enqueue(arg + "\n" + "\n");
startInfo.WorkingDirectory = #"\cygdrive\c\Users\jhb\Source\Repos\myprogramm";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
process.Start();
If I set startInfo.WorkingDirectory to a windows path the program will run, but the command does not run in the path I set.
I want to redirect standardoutput of a Process in a richTextBox. Here is my process configuration,
string command = "/K perl C:\\Server.pl ";
ProcessStartInfo startInfo = new ProcessStartInfo();
Process proc = new Process();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
proc.StartInfo = startInfo;
proc.OutputDataReceived += (s, ea) => this.richTextBox1.AppendText(ea.Data);
proc.Start();
proc.BeginOutputReadLine();
Here is my Server.pl file
print "Server1 \n";
while(1)
{
print "Server \n";
sleep 1;
}
But when I run the program the cmd.exe is just black and nothing printed in richTextBox. but when I change the
startInfo.RedirectStandardOutput = false;
I have this out put in my cmd.exe:
Server1
Server
Server
Server
...
How I can work around this issue ?
Might be as simple as disabling output buffering in your perl script. This is done using the $| special variable (see perlvar).
$| = 1;
print "Server1 \n";
...