child process window not appearing - c#

I am trying to create a child process from a c# console app that shows the console. I tried the following but no window appeared.
ProcessStartInfo = new ProcessStartInfo(name)
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Maximized,
CreateNoWindow = false,
ErrorDialog = false
};
if (args != null)
{
ProcessStartInfo.Arguments = args;
}
if (workingDirectory != null)
{
ProcessStartInfo.WorkingDirectory = workingDirectory;
}
Process = new Process {EnableRaisingEvents = true, StartInfo = ProcessStartInfo};
Process.Start();

The right way to run child process in the parent’s console is to setup UseShellExecute property of ProcessStartInfo class. Let’s consider an example that executes time command. Why time? Because it reads from standard input. This way you will know which console it uses.
public class Program
{
public static void Main(string[] args)
{
var processInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c time"
};
Console.WriteLine("Starting child process...");
using (var process = Process.Start(processInfo))
{
process.WaitForExit();
}
}
}
We left the default value of UseShellExecute, which is true. It means that a shell will be used for the child process. Using shell means that a new console will be created.
Let’s flip the value of UseShellExecute to false.
public class Program
{
public static void Main(string[] args)
{
var processInfo = new ProcessStartInfo
{
UseShellExecute = false, // change value to false
FileName = "cmd.exe",
Arguments = "/c time"
};
Console.WriteLine("Starting child process...");
using (var process = Process.Start(processInfo))
{
process.WaitForExit();
}
}
}

Related

How to run this app with hidden or even minimized console?

var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"C:\Program Files\CPUID\CPU-Z\cpuz.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
for(; ; )
{
process.Start();
Thread.Sleep(20000);
process.Kill();
}
This program need to be hidden or even minimized?
Can someone explain to me how to upgrade this?
Add these lines to your code
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;

How to restart a C# console app in the same Cmd window? [duplicate]

Can I start a process (using C# Process.Start()) in the same console as the calling program? This way no new window will be created and standard input/output/error will be the same as the calling console application. I tried setting process.StartInfo.CreateNoWindow = true; but the process still starts in a new window (and immediately closes after it finishes).
You shouldn't need to do anything other than set UseShellExecute = false, as the default behaviour for the Win32 CreateProcess function is for a console application to inherit its parent's console, unless you specify the CREATE_NEW_CONSOLE flag.
I tried the following program:
private static void Main()
{
Console.WriteLine( "Hello" );
var p = new Process();
p.StartInfo = new ProcessStartInfo( #"c:\windows\system32\netstat.exe", "-n" )
{
UseShellExecute = false
};
p.Start();
p.WaitForExit();
Console.WriteLine( "World" );
Console.ReadLine();
}
and it gave me this output:
You could try redirecting the output of this process and then printing it on the calling process console:
public class Program
{
static void Main()
{
var psi = new ProcessStartInfo
{
FileName = #"c:\windows\system32\netstat.exe",
Arguments = "-n",
RedirectStandardOutput = true,
UseShellExecute = false
};
var process = Process.Start(psi);
while (!process.HasExited)
{
Thread.Sleep(100);
}
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
}
Alternative approach using the Exited event and a wait handle:
static void Main()
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo
{
FileName = #"netstat.exe",
Arguments = "-n",
RedirectStandardOutput = true,
UseShellExecute = false
};
p.EnableRaisingEvents = true;
using (ManualResetEvent mre = new ManualResetEvent(false))
{
p.Exited += (s, e) => mre.Set();
p.Start();
mre.WaitOne();
}
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
}

How to prevent a process started by Process.Start() from exiting when my app exits?

my method...
private static void RunAndExit(string command)
{
var processInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c " + command,
CreateNoWindow = false,
UseShellExecute = true,
RedirectStandardError = false,
RedirectStandardOutput = false
};
}
I want the process started by RunAndExit() to continue to run after the app containing this method has exited. Thanks for any help!
Your code will already achieve that, so long as you actually start the new process. You are missing a call to Process.Start().
private static void RunAndExit(string command)
{
var processInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
....
};
Process.Start(processInfo); // add this line
}

Windows Form Application with Console Application

I have a console application that asks for a SourcePath when started.. when I enter the Source Path, It asks for DestinationPath... when i enter DestinationPath it starts some execution
My Problem is to Supply these path via a windows application, means i need to create a window form application that will supply these parameters to the console application automatiocally after certain time interval
can it be achieved or not... if yes, plese help... its very Urgent...
ohh..
I have tried a lot of code that i can not paste hear all but some that i use to start the application are...
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = #"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe";
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.CreateNoWindow = false;
psi.Arguments = input + ";" + output;
Process p = Process.Start(psi);
and
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = #"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
if (process.Start())
{
Redirect(process.StandardError, text);
Redirect(process.StandardOutput, text);
MessageBox.Show(text);
}
private void Redirect(StreamReader input, string output)
{
new Thread(a =>{var buffer = new char[1];
while (input.Read(buffer, 0, 1) > 0)
{
output += new string(buffer);
};
}).Start();
}
but nothing seems to be working
You can add parameters to your ProcessStartInfo like this:
ProcessStartInfo psi = new ProcessStartInfo(#"C:\MyConsoleApp.exe",
#"C:\MyLocationAsFirstParamter C:\MyOtherLocationAsSecondParameter");
Process p = Process.Start(psi);
this will startup the console app with 2 parameters.
Now in your console app you have the
static void Main(string[] args)
the string array args is what contains the parameters, now all you have to do is get them when your app starts.
if (args == null || args.Length < 2)
{
//the arguments are not passed correctly, or not at all
}
else
{
try
{
yourFirstVariable = args[0];
yourSecondVariable = args[1];
}
catch(Exception e)
{
Console.WriteLine("Something went wrong with setting the variables.")
Console.WriteLine(e.Message);
}
}
This may or may not be the exact code that you need, but at least will give you an insight in how to accomplish what you want.

Process to call CLI exe not returning output

I have a app that is doing some video processing.
I need to analyze the media before processing it.
The ffmpeg utility ffprobe.exe provides all the information I need.
However the code I am using will not return the text that appears when the command is ran in a cmd window :
public static string RunConsoleCommand(string command, string args)
{
var consoleOut = "";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
process.Start();
consoleOut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return consoleOut;
}
}
Any Ideas?
The Process class has some events to handle that :
public static string RunConsoleCommand(string command, string args)
{
var consoleOut = "";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
// Register for event and do whatever
process.OutputDataReceived += new DataReceivedEventHandler((snd, e) => { consoleOut += e.Data; });
process.Start();
process.WaitForExit();
return consoleOut;
}
}
You also have ErrorDataReceived, which works the same way.
I'm using these events in some projects, and it works like a charm. Hope that helps.
EDIT : Fixed the code, you need to attach the handlers before you start the process.

Categories

Resources