Windows Form Write to Console - c#

I am simply trying to open the console and write a single line and execute it with the console staying open once the line is written. Currently, the command line is opening blank and not writing anything. Any way to fix this?
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = false;
cmd.Start();
cmd.StandardInput.WriteLine("echo hello");
cmd.StandardInput.WriteLine("pause");
cmd.WaitForExit();

This will fix it:
cmd.StartInfo.RedirectStandardOutput = false;
This works perfect for me:
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
//cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start();
cmd.StandardInput.WriteLine("pause");
System.Threading.Thread.Sleep(5000);
cmd.StandardInput.WriteLine(" ");
cmd.StandardInput.WriteLine("dir /p");
cmd.StandardInput.WriteLine("exit");
cmd.WaitForExit();
}
}
}

You could most likely just issue a pause:
cmd.StandardInput.WriteLine("pause");
Should solve it.
Remember to add:
cmd.StartInfo.RedirectStandardInput = false;
If that does not work then most likely still an issue, as reported at Microsoft regarding CMD specifically and output. See: http://connect.microsoft.com/VisualStudio/feedback/details/609801/unable-to-redirect-only-the-standard-input-of-process-cmd-exe-or-batch-file-from-windows-form-application

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/K \"echo hello\"";
cmd.Start();

Related

Problems using cmd with unity

I'm trying to run command prompt from unity, my problem is that I can't find a way to fetch the result, here's my code
Process myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe";
string path = "C:\\Users\\Sam\\Desktop\\Test\\rubik3Sticker.generator";
myProcess.StartInfo.Arguments = "call " + path;
myProcess.EnableRaisingEvents = true;
myProcess.Start();
until here it gives no exceptions, but unity crashes when I try to add this:
while(!myProcess.StandardOutput.EndOfStream)
print(myProcess.StandardOutput.ReadLine());
any kind of help would be appreciated, thanks!

How to auto fill cmd prompt

I Have a C# program; when I hit a button I want it to open a CMD window, then automatically type in the cmd window and run that said command. So far I have this from 4 hours of research. But nothing is working.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
//p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ipconfig");
Any idea on how to fill in a certain text then automatically run it when the button is hit?
With StandardInput and StandardOutput redirected, you cannot see the new window opened. If you want to create a new cmd window and run ipconfig in it, you could do this:
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c ipconfig & pause";
p.Start();
I agree that if all you want to do is execute "ipconfig" you could just invoke it instead of cmd.exe. Assuming you want to do other things with cmd.exe, here is an example of how to invoke it, have it execute a command, and then terminate (using the /K switch instead of /C will keep cmd.exe running):
using System;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C ipconfig";
p.Start();
var output = p.StandardOutput.ReadToEnd();
Console.Write(output);
Console.ReadKey();
}
}
}

windows forms to run commands

I am trying to launch a new cmd process, from that run a batch file to setup environments and from that run custom commands. Is this possible?
So far I have:
Process cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.Filename = <setup.cmd path>
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start()
this successfully set up the environment but the cmd window immediately closes and i can't submit more commands.
Simply add the ProcessStartInfo.Arguments and pass "/K" as value
Process cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/K";
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start();
Passing the argument /K will force the command window to remain open
You can add also the name of your batch file after the /K
cmd.StartInfo.Arguments = "/K yourbatch.cmd args1 args2";

Hide console window from Process.Start C#

I am trying to create process on a remote machine using using System.Diagnostics.Process class.
I am able to create a process. But the problem is, creating a service is take a long time and console window is displayed.
Another annoying thing is the console window is displayed on top of my windows form and i cant do any other operations on that form.
I have set all properties like CreateNoWindow = true,
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
but still it shows the console window. even i have redirected output and errors to seperate stream but no luck.
Is there any other way to hide the Console window? Please help me out .
Here is the part of my code i used to execute sc command.
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "sc";
proc.StartInfo.Arguments = string.Format(#"\\SYS25 create MySvc binPath= C:\mysvc.exe");
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
I had a similar issue when attempting to start a process without showing the console window. I tested with several different combinations of property values until I found one that exhibited the behavior I wanted.
Here is a page detailing why the UseShellExecute property must be set to false.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx
Under Remarks section on page:
If the UseShellExecute property is true or the UserName and
Password properties are not null, the CreateNoWindow property
value is ignored and a new window is created.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fullPath;
startInfo.Arguments = args;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
try
{
processTemp.Start();
}
catch (Exception e)
{
throw;
}
I've had bad luck with this answer, with the process (Wix light.exe) essentially going out to lunch and not coming home in time for dinner. However, the following worked well for me:
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// etc, then start process
This should work, try;
Add a System Reference.
using System.Diagnostics;
Then use this code to run your command in a hiden CMD Window.
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Enter your command here";
cmd.Start();
This doesn't show the window:
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.CreateNoWindow = true;
...
cmd.Start();

How do I execute command in c#?

I want to execute command to call microphone config.
control mmsys.cpl,,1
I try following code but I throw Win32Exception, The system cannot find the file specified.
Process.Start("control mmsys.cpl,,1");
It should be:
Process.Start("control","mmsys.cpl,,1")
Documentation: http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx
You can try this -
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
/* execute "dir" command */
cmd.StandardInput.WriteLine("dir");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
cmd.Close();
You can write your command to a bat file and execute it from the command prompt using the above method.

Categories