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!
Related
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();
I am trying to execute shell commands [which is supposed to be in cygwin's sh.exe] through a c# program.
Process proc = new Process();
ProcessStartInfo procStartInfo = new ProcessStartInfo(#"C:\cygwin64\bin\sh.exe", "history");
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
proc.StartInfo = procStartInfo;
proc.Start();
But i'm getting the following error instead of getting the list of commands
/usr/bin/sh: history: No such file or directory
Can you please let me know what i'm missing here?
Thanks
I know this question has been asked previously and I have tried all the solutions given in those posts before but I can't seem to make it work:-
static void CallBatch(string path)
{
int ExitCode;
Process myProcess;
ProcessStartInfo ProcessInfo;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + path);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
myProcess = Process.Start(ProcessInfo);
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.WaitForExit();
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(process_Exited);
ExitCode = myProcess.ExitCode;
Console.WriteLine("ExitCode: " + ExitCode.ToString(), "ExecuteCommand");
myProcess.Close();
}
When I try to call the batch file, it still shows the window even though createNoWindow and UseShellExecute are both set to true.
Should I put something else to make it run the batch file silently?
Try this:
Process myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c " + path;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(process_Exited);
myProcess.Start();
myProcess.WaitForExit();
ExitCode = myProcess.ExitCode;
The idea is to not manipulate myProcess.StartInfo after you have started your process: it is useless. Also you do not need to set UseShellExecute to true, because you are starting the shell yourself by calling cmd.exe.
I have a very simple batch file:
echo Text > Test.txt
This file is saved here:
R:\Testing123.bat
full UNC pathway is
\\imfile\depart$\DB\Testing123.bat
In my console application the following runs:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.WorkingDirectory = #"R:\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
This does not run if I use the full pathway:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true ;
myProcess.StartInfo.WorkingDirectory = #"\\imfile\depart$\DB\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Can I not use these UNC pathways for the WorkingDirectory property? I thought when programming it was always best practice to use these pathways?
EDIT
Using one of the suggestions I now have the following which unformtunately still doesn't work :
{
Process myP = new Process();
myP.StartInfo.UseShellExecute = false;
string myString = #"pushd \\imfile\depart$\DB";
myP.StartInfo.FileName = "cmd";
myP.StartInfo.Arguments = myString;
myP.StartInfo.CreateNoWindow = true;
myP.Start();
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true ;
myProcess.StartInfo.WorkingDirectory = #"\\imfile\depart$\DB";
//myProcess.StartInfo.WorkingDirectory = #"R:\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
bat-files are executed via cmd.exe, which (by default) does not allows UNC paths as "working directory". However, you may change this behavoir via registry.
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"DisableUNCCheck"=dword:00000001
http://support.microsoft.com/kb/156276
This information is known to me from Far Manager TechInfo Section 2.2.
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();