How to launch firefox app from c# in ubuntu - c#

c-sharpers, I am trying to use "System.Diagnostics.Process" to open the firefox app, but can't figure it out in ubuntu 20.04
main.cs
using var process = new Process();
process.StartInfo.FileName="/lib/firefox/firefox.sh";
process.StartInfo.UseShellExecute = false;
process.Start();
After the execution of the code above, nothing happens no error no dust ??

The solution that I found is to add "Thread.sleep()" .
static void Main(string[] args)
{
using var process = new Process();
process.StartInfo.FileName="/usr/lib/firefox/firefox.sh";
process.StartInfo.UseShellExecute = false;
process.Start();
Thread.Sleep(1000);
}

Related

C# Realtime standard output/error capture of a process

I'm developing a C# application and I need to run an external console process (for example a python script) and receive the output data of the script in real-time. The python script is something like this:
import time
while 1:
print("Hi Foo!")
time.sleep(.3)
The following C# code prints the python script output on the C# console in real-time:
static void Main(string[] args)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "test.py";
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
}
However, when I try to capture the output data and write them on the console manually, I fail. The recommended solution according to other posts is something like this, but it doesn't work:
static void Main(string[] args)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "test.py";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
}
The process.StandardOutput.ReadToEnd() works in blocking mode waiting for the process to exit and returns the whole output all at once. What exactly is the problem with the real-time output capturing and how can I fix it?
using (var process = new Process())
{
process.StartInfo.FileName = #"python.exe";
process.StartInfo.Arguments = "-u test.py";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
Console.WriteLine(line);
// do something with line
}
process.WaitForExit();
Console.Read();
}

.NET System.Process: when you redirect StandardError, console window receives no output

I want to get the error output of a console program in case it crashes. But I want the standard output to display in the console window. However, if I redirect the standard error, nothing is output to the console window.
Dim p As New Process
p.StartInfo.Filename = filename
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.EnableRaisingEvents = True
p.Start
p.BeginReadErrorLine
So now if the launched process (a console program) crashes, I get the error output as expected. However, the standard output is not visible in the console program.
I wrote a test program to launch.
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
object m = null;
string s = m.ToString();
}
I expect to see the numbers 0 - 999 scroll in the console window and get the error output through the redirect, but I see nothing in the console window unless nothing is redirected.
I haven't found any questions dealing with this issue. Is this a defect or something i've overlooked?
I tried reproducing same, But I couldn't. Here's my code where I am redirecting only error but still I am able to see output on my console window:
public class Program
{
static void Main(string[] args)
{
ExecuteCommand(#"cmd.exe", #"/c C:\Users\*****\Documents\Test\ConsoleApp1.exe");
}
public static int ExecuteCommand(string exePath, string cmdText)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = exePath;
startInfo.Arguments = cmdText;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode;
}
}
Here's my output:
Am I doing something different form what You have tried?

C# Process Start Stuck

I wrote a simple TCP Server application,
when the program gets the request it will call an exe file to do something.
The application works fine on my PC.
But when I run on another PC the thread will stuck because it stuck in process start, so I checked the task manager whether the exe file did run. and it really work.
I have no idea how to solve it.
Here is my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
string path = System.Environment.CurrentDirectory;
process.StartInfo.WorkingDirectory = path;
process.StartInfo.FileName = "FileTransferTool\\FileTransfer.exe";
process.StartInfo.Arguments = argument;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = false;
process.Start(); // the thread stuck here
I print out the message, it shows
System.ComponentModel.Win32Exception (0x80004005):
The system cannot find the file specified at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startIn
fo)
at System.Diagnostics.Process.Start() at ClassPackage.DsUploadFiletoNC.Start(Object[] param)
I have solved this problem by adding another process to call cmd.exe on the top of the main program, the code shown as below :
And the program will run as Administrator, but I don't know why
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
string path = System.Environment.CurrentDirectory;
process.StartInfo.WorkingDirectory = #"C:\Windows\System32";
process.StartInfo.FileName = "cmd.exe";
//process.StartInfo.FileName = "FileTransferTool\\circle.txt";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardInput = true;
//process.StartInfo.CreateNoWindow = false;
process.Start();
using (TCPServer server = new TCPServer())
{
server.Start();
Console.WriteLine("Socket Server Start...");
Console.ReadKey(true);
}
}

C# Console application not showing console output when starting from different application

I am using a windows application to start a console application for command line parameters configuration.
When I am sending the command line parameters through debug mode, the application is working perfect, and all Console.WriteLine is printing to console, but when am starting the process from windows application of that console application it is not showing console output
the way, am starting the process is
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = EXEName;
procStartInfo.Arguments = FilePath;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
using (Process process = new Process())
{
process.StartInfo = procStartInfo;
process.Start();
}
You need to set ProcessStartInfo.RedirectStandardOutput to false;

How can I start a process in the background?

I can't seem to find an answer on Google or here on StackOverflow.
How can I start a process in background (behind the active window)? Like, when the process starts, it will not interrupt the current application the user is using.
The process won't pop out in front of the current application, it will just start.
This is what I'm using:
Process.Start(Chrome.exe);
Chrome pops up in front of my application when it's started. How can I make it start in background?
I've also tried:
psi = new ProcessStartInfo ("Chrome.exe");
psi.UseShellExecute = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(psi);
But there's no difference at all from the previous one.
Thanks.
Try this:
Process p = new Process();
p.StartInfo = new ProcessStartInfo("Chrome.exe");
p.StartInfo.WorkingDirectory = #"C:\Program Files\Chrome";
p.StartInfo.CreateNoWindow = true;
p.Start();
Also, if that doesn't work, try adding
p.StartInfo.UseShellExecute = false;
Below code should do what you need:
class Program
{
static void Main(string[] args)
{
var handle = Process.GetCurrentProcess().MainWindowHandle;
Process.Start("Chrome.exe").WaitForInputIdle();
SetForegroundWindow(handle.ToInt32());
Console.ReadLine();
}
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
}

Categories