Before you say its a duplicate question, please let me explain (as I've read all similar threads).
My application has both of these settings:
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
and is also has WindowsApplication as the output type.
The black window STILL comes up when I call a command line command. Is there anything else I can do to hide the window? It doesn't happen for all commands, XCOPY is a situation where it the black window does flash up. This only happens though when the destination I'm XCOPYing too already contains the file and it's prompting me if I want to replace it. Even if I pass in /Y it will still flash briefly.
I'm open to using vbscript if that will help, but any other ideas?
The client will call my executable and then pass in a command line command ie:
C:\MyProgram.exe start XCOPY c:\Test.txt c:\ProgramFiles\
Here's the full code of the application:
class Program
{
static void Main(string[] args)
{
string command = GetCommandLineArugments(args);
// /c tells cmd that we want it to execute the command that follows and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = procStartInfo;
process.Start();
}
private static string GetCommandLineArugments(string[] args)
{
string retVal = string.Empty;
foreach (string arg in args)
retVal += " " + arg;
return retVal;
}
}
The problem is that you're using cmd.exe. Only its console window will be hidden, not the console window for the process you ask it to start. There's little point in using cmd.exe, unless you are trying to execute some of the commands it implements itself. Like COPY.
You can still suppress the window if you need cmd.exe, you'll have to use the /B option for Start. Type start /? at the command prompt to see options. Not that it helps, you can't use START COPY.
There's a specific quirk in xcopy.exe that might throw you off as well. It does not execute if you don't also redirect the input. It just fails to run without diagnostic.
i see that you are calling cmd and then passing the command as parameters. Instead call the command directly
e.g.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.DiagnosticsProcessStartInfo("xcopy", "<sourcedir> <destdir> <other parameters>");
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
You can try adding
process.StartInfo.UseShellExecute = false;
to your process
If you are calling cmd.exe in your C# code and passing the commands to it via standard input.WriteLine and you don't want your CMD window to pop up every time you run your code, you can simply write this command:
test.StartInfo.FileName = "cmd.exe";
test.StartInfo.CreateNoWindow = true;
By setting create no window to false, we are running the command sent to the CMD in the background and the output is not being displayed to the user. By setting it to false, the CMD window pops up.
I had a similar task - It is possible to hide the window after creation via an API call.
(In your case you maybe need a helper thread.)
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
If you know the handle of the new Window you can call
ShowWindow(hWnd, 0);
0 hides the window, 1 shows the window
To get the handle of the Window take a look at:
pinvoke.net enumwindows(user32)
Related
My application was built on .NET Windows Forms
I have to convert the application to work as web application now.
Many fuctions have worked with no change what so ever
I used to have a function that executes command and opens command prompt window.
I put the code in ASP.net web application, the code is fine and commands get executed but the problem is that command prompt window does not show any more.
The code has not changed.
I wonder how can I get command prompt window appear in the server-side so admin can know if there is command error or any other issues?
This is the function that I use to execute a command
static void ExecuteCommand(string command, string workingFolder)
{
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = workingFolder;
var process = Process.Start(processInfo);
process.WaitForExit();
}
From the documentation you can keep open the command window with "/K" flag
If the fileName parameter represents a command (.cmd) file, the
arguments parameter must include either a "/c" or "/k" argument to
specify whether the command window exits or remains after completion.
var processInfo = new ProcessStartInfo("cmd.exe", "/k " + command);
processInfo.WorkingDirectory = workingFolder;
var process = Process.Start(processInfo);
process.WaitForExit();
But in order to get the error and show it to your admin you have to use StandardError and you can see the examples in the documentation.
I'm trying to do execute an external .bat by pressing a button.
The intention is to call some XCOPY instructions. Therefore I execute "sync.bat" using Process.Start(startInfo).
The output of that .bat is redirected to my App and shown in a dialog box. My code waits until the external call has finished.
echo "Batch SYNC started."
pause
xcopy "e:\a\*" "e:\b\" /f /i /c /e /y
pause
echo "Batch SYNC finished."
OK:
When I build my program as "release" and start it within VisualStudio2013, everything works fine (I see the Results, have to press ENTER in the black window, files are copied).
FAIL:
When I start my app by double-click (in file-explorer or from the desktop) or a debug build within the VisualStudio, I see the ECHO and the PAUSE output, but the batch did not stop and I see no results from XCOPY. Seems as if the PAUSE and XCOPY are killed immediately.
I got no exception and no entry in Windows-log.
I have tried to make DEBUG and RELEASE configuration identical (with no success).
Does anybody have an idea what I may do to get this simple function work outside the IDE?
Here is the code of the function called when the button is pressed:
private void ProcessSync_bat()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = "sync.bat";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = "";
startInfo.ErrorDialog = true;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
dlgFKSyncMessageBox.AddLine("----------sync.bat started-----------");
dlgSyncMessageBox.AddLine("===============Result================");
while (!exeProcess.StandardOutput.EndOfStream)
{
dlgSyncMessageBox.AddLine(exeProcess.StandardOutput.ReadLine());
}
dlgSyncMessageBox.AddLine("===============ERRORS================");
while (!exeProcess.StandardError.EndOfStream)
{
dlgSyncMessageBox.AddLine(exeProcess.StandardError.ReadLine());
}
exeProcess.WaitForExit();
}
}
catch (Exception exp)
{
dlgSyncMessageBox.AddLine("========EXCEPTION========");
}
}
Solution:
If I additionally set
startInfo.RedirectStandardInput = true;
then it works. I may redirect input from dialog window to the Process. Since I do not need any input for the intended XCOPY, this solution works for me without catching chars from the Dialog window and Forward to the process.
I can't see the logic, why I have to Redirect input too, but I'm glad that my Software now does what I need.
Sorry if this is something obvious, I'm super new to C#.
I'm working on a program that runs a python script to check something online, that then writes it back to a file that C# reads and puts on a form. Everything works if I manually run them both at the same time, but I really want to start the script from the C# program.
Here's the function that should start the python script:
private void RunPython()
{
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "python " + path + "\\foo.py";
p.StartInfo = startInfo;
p.Start();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// get things set up... etc.
RunPython();
}
I've tried running it without the window hidden, and it just brings up an empty cmd line in the 'path' directory. Is it never running the python script? It doesn't seem like it is, but it may also be running it and immediately closing it. I can't tell.
I need it to stay open for the duration of the C# program's run, so if the problem is it exiting when the RunPython() method returns, is there some different way I could go about this?
Thanks!!
If you want to run a program in the command line using arguments to cmd.exe you need the /c flag. Alternatively you can use /k but that keeps the command process running which you probably don't want.
/c Carries out the command specified by String and then stops.
So it's usage is cmd.exe /c [string]
Try changing your arguments line to:
startInfo.Arguments = "/c \"python " + path + "\\foo.py\"";
See here for more information about running cmd.exe and dealing with quotes in the string section of the command: https://technet.microsoft.com/en-us/library/cc771320(v=ws.11).aspx
Am a Newbie in C# and I have 3 commands(command2, command3 and command4) I need to execute in the elevated command prompt and I will also like to view the execution process as it happens. Currently, the problem is that the code below just opens the elevated command prompt and without executing the commands. I also seek better interpretations of the lines if wrong.
My code and Interpretation/Understanding of each line based on reviews of similar cases: ConsoleApp1
class Program
{
static void Main(string[] args)
{
string command2 = #"netsh wlan";
string command3 = #" set hostednetwork mode=true ssid=egghead key=beanhead keyusage=persistent";
string command4 = #" start hostednetwork";
string maincomm = command2.Replace(#"\", #"\\") + " " + command3.Replace(#"\", #"\\") ; //I merged commands 2 and 3
ProcessStartInfo newstartInfo = new ProcessStartInfo();
newstartInfo.FileName = "cmd"; //Intend to open cmd. without this the newProcess hits an error saying - Cannot run process without a filename.
newstartInfo.Verb = "runas"; //Opens cmd in elevated mode
newstartInfo.Arguments = maincomm; //I intend to pass in the merged commands.
newstartInfo.UseShellExecute = true; //
newstartInfo.CreateNoWindow = true; // I intend to see the cmd window
Process newProcess = new Process(); //
newProcess.StartInfo = newstartInfo; //Assigns my newstartInfo to the process object that will execute
newProcess.Start(); // Begin process and Execute newstartInfo
newProcess.StartInfo.Arguments = command4; //I intend to overwrite the initial command argument hereby passing the another command to execute.
newProcess.WaitForExit(); //
}
}
This is what I did to overcome the challenge and It gave me exactly what I wanted. I modified my code to use the System.IO to write directly to the elevated command prompt.
ProcessStartInfo newstartInfo = new ProcessStartInfo();
newstartInfo.FileName = "cmd";
newstartInfo.Verb = "runas";
newstartInfo.RedirectStandardInput = true;
newstartInfo.UseShellExecute = false; //The Process object must have the UseShellExecute property set to false in order to redirect IO streams.
Process newProcess = new Process();
newProcess.StartInfo = newstartInfo;
newProcess.Start();
StreamWriter write = newProcess.StandardInput ; //Using the Streamwriter to write to the elevated command prompt.
write.WriteLine(maincomm); //First command executes in elevated command prompt
write.WriteLine(command4); //Second command executes and Everything works fine
newProcess.WaitForExit();
Referrence: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx
I think an understanding of some properties of the ProcessStartInfo might clear things.
The verb - Gets or sets the verb to use when opening the application or document specified by the FileName property.,
+The UseShellExecute - Gets or sets a value indicating whether to use the operating system shell to start the process.
+The FileName - Gets or sets the application or document to start MSDN Docs
When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, by using the Process object. When UseShellExecute is false, you can start only executables by using the Process object Documentation from MSDN.
In my case, cmd is an executable. the verb property is some thing that answers the question "How should my I run my FileName(for executables e.g cmd or any application)?" for which I answered - "runas" i.e run as administrator. When the FileName is a document (e.g `someFile.txt), the verb answers the question "What should I do with the file for which answer(verb) could be -"Edit","print" etc. also?"
use true if the shell should be used when starting the process; false if the process should be created directly from the executable file. The default is true MSDN Docs - UserShellInfo.
Another thing worth noting is knowing what you are trying to achieve. In my case, I want to be able to run commands via an executable(cmd prompt) with the same process - i.e starting the cmd as a process I can keep track of.
I would like to mimic the Run command in Windows in my program. In other words, I would like to give the user the ability to "run" an arbitrary piece of text exactly as would happen if they typed it into the run box.
While System.Diagnostics.Process.Start() gets me close, I can't seem to get certain things like environment variables such as %AppData% working. I just keep getting the message "Windows cannot find '%AppData%'..."
You can use the Environment.ExpandEnvironmentVariables method to turn %AppData% into whatever it actually corresponds to.
Depending on what you're trying to do, you could also call CMD.EXE, which will expand your environment variables automatically. The example below will do a DIR of your %appdata% folder, and redirect the stdOut to the debug:
StreamReader stdOut;
Process proc1 = new Process();
ProcessStartInfo psi = new ProcessStartInfo("CMD.EXE", "/C dir %appdata%");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
proc1.StartInfo = psi;
proc1.Start();
stdOut = proc1.StandardOutput;
System.Diagnostics.Debug.Write(stdOut.ReadToEnd());