I have faced a strange problem when trying to run a process from hidden window - the process I run runs in hidden like my process, Am I doing something wrong? I want to run that child process not hidden.
Process.Start(Path.GetTempPath() + "cleanup.exe", Path.GetDirectoryName(Application.StartupPath);
you can try with creating an object of Process class as below :
Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// Passing the batch file/exe name
objProcess.StartInfo.FileName = string.Format(strBatchFileName);
// Passing the argument
objProcess.StartInfo.Arguments = string.Format(strArgument);
try
{
objProcess.Start();
}
catch
{
throw new Exception("Batch file is not found for processing");
}
Related
I have a method that running an EXE file using cmd. This EXE file getting an exception during its initializing, then retrying and starting a process on my system. I cannot edit the EXE file to change its behavior.
when running the code, after the exception, the command line window closed immediately.
How can I keep the cmd window open till the retry?
Thanks,
Tal.
public void RunJob(Process cmdProcess)
{
cmdProcess.StartInfo.FileName = "cmd.exe";
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.StartInfo.CreateNoWindow = true;
cmdProcess.StartInfo.RedirectStandardOutput = true;
cmdProcess.OutputDataReceived += new DataReceivedEventHandler(form.SortOutputHandler);
cmdProcess.StartInfo.RedirectStandardError = true;
cmdProcess.ErrorDataReceived += new DataReceivedEventHandler(form.SortOutputHandler);
cmdProcess.StartInfo.RedirectStandardInput = true;
cmdProcess.StartInfo.Arguments = string.Format("/k cd \"{0}\\BinFolder\"", form.txtLocalRepo);
cmdProcess.StartInfo.Verb = "runas";
cmdProcess.Start();
StreamWriter cmdStreamWriter = cmdProcess.StandardInput;
cmdProcess.BeginOutputReadLine();
cmdStreamWriter.WriteLine("START testing.exe");
}
If this is a Console Application, then you might consider using the new Async Main method. You could combine this with something like TaskCompletionSource so you can flag your app as complete when you're ready, and have it wait until you flag the task as complete.
I have a winforms application where I run a process (code shown below) in a loop. Suppose I have 10 items, I get 10 command windows where the actions are run. So, I wanted to know if there is a way where I can run the process 10 times but have just one command window open where all my actions are run.
Process p = new Process();
try
{
var pInfo = new ProcessStartInfo(executable, args);
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.WorkingDirectory = workingDir;
p.StartInfo = pInfo;
p.EnableRaisingEvents = true;
SubscribeEvents(p);
p.Start();
p.WaitForExit();
}
I guess if you are doing winforms, you actually already probably have a main window open.
You can use this to hide your other windows:
pInfo.CreateNoWindow = true;
Hi am creating a console application that executes another windows console application.
Sometimes the application never end its execution, an I need to force kill it, if exceeded a time. When the application is force killed I am unable to get the output of the application.
There is a way to get the output before killing the running process in order to write to a file or get it after killing it?
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "robocopy.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.Arguments = parameters;
process.Start();
//set the maximum allowed time in which the robocopy lives
if (process.WaitForExit(15000))
{
logInfo = process.StandardOutput.ReadToEnd();
//do something else
}
else
{
process.Kill();
logInfo = process.StandardOutput.ReadToEnd();
//write the output to a file
}
I came across this question, which looked like it would resolve what I'm trying to do, and I'm trying to use similar code where a Process() object is created and the "sc" command is called from code.
static void SetRecoveryOptions(string serviceName)
{
int exitCode;
using (var process = new Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = "sc";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
// tell Windows that the service should restart if it fails
startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= restart/60000", serviceName);
process.Start();
process.WaitForExit();
exitCode = process.ExitCode;
}
if (exitCode != 0)
throw new InvalidOperationException();
}
I've tried calling that code from a few locations (such as the committed event handler for the service installer, OnStart of the service itself, etc) but every time I get an exception as soon as the Process() object is created. The exception is: "operation is not allowed due to the current state of the object".
Any ideas what I'm missing here?
When working with a command line program, via a c# class method.
How do you determine if the commandline program was successfully executed and the operation it has performed is ok or has failed?
Also how do you get the screen commandline output into the c# class method?
You can use the Process class to execute a command line command.
The following code captures the standard output to output, and assigns the processes exit code to exitCode.
using (Process p = new Process())
{
p.StartInfo.FileName = exeName;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
int exitCode = p.ExitCode;
}
Something like:
Process mycommand = new Process();
mycommand.StartInfo.FileName = "myexe.exe";
mycommand.StartInfo.Arguments = "param1";
mycommand.StartInfo.UseShellExecute = false;
mycommand.StartInfo.RedirectStandardOutput = true;
mycommand.Start();
Console.WriteLine(mycommand.StandardOutput.ReadToEnd());
mycommand.WaitForExit();
You usually determine an exe's state wether the exit code is 0, but that is arguably down to the writer of the exe
I assume you're using the Process class to call the command line app.
You can find the exit code of the process using Process.ExitCode. You can redirect its standard output by setting ProcessStartInfo.RedirectStandardOutput before starting it, and then either using Process.StandardOutput or the Process.OutputDataReceived event.
Take a look at this questionenter link description here.
The additional information you might need is process.ExitCode to see if it was sucessful. Of course, the Main method of the console app must return an exit code when it is unsuccessful, which many do not.
For this, you use the Process.Start method. You can control how the process runs with the passed in ProcessStartInfo:
var myProcess = Process.Start(new ProcessStartInfo {
FileName = "process.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
if (!myProcess.WaitForExit(5000)) { // give it 5 seconds to exit
myProcess.Kill();
}
if (myProcess.ExitCode != 0) {
// error!
}
var output = myProcess.StandardOutput.ReadToEnd(); // access output