How to open and detect closing of an exe in c# - c#

I am developing a Windows Forms application in which I need to edit certain config files. Now when the user clicks on the edit option, I want to launch these config files in a simple notepad editor. Once launched I want to stall my application. Only when the user closes the notepad editor, I want to un-stall my application. How can this be done ?
I have seen these questions, but the answers have many issues. (I read the comments given there.)
Q1Q2

You can use the Exited-Event:
try
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.Arguments = #"C:\PathToYourFile";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
//Handle ERROR
return;
}
// Handle Exited event and display process information.
private void myProcess_Exited(object sender, System.EventArgs e)
{
eventHandled = true;
Console.WriteLine("Exit time: {0}\r\n" +
"Exit code: {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
}

var process = System.Diagnostics.Process.Start("yourfile.txt");
process.WaitForExit();
This will open your file. However, sometimes the process will be null and you will not be able to wait for exit. Why?
Process.Start Method
Return Value Type: System.Diagnostics.Process
A new Process component
that is associated with the process resource, or null if no process
resource is started (for example, if an existing process is reused).
http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

You can initiate a Process and WaitForExit:
Process pr = Process.Start("Notepad");
pr.WaitForExit();

Related

How to start another application from .NET solution and exit only the first one

I'm writing a software and trying to implement an automatic updater into it as a separate application. I have both the software itself and the updater under the same solution.
When an update is available from a server, my program prompts the user to update, this part works.
I fail to call the updater app and then exit only the first program.
This is what I have not:
private void button2_Click(object sender, EventArgs e)
{
// Update
ExecuteAsAdmin("AutoUpdater.exe");
//System.Windows.Forms.Application.Exit();
Process.GetCurrentProcess().Kill();
}
public void ExecuteAsAdmin(string fileName)
{
Process proc = new Process();
proc.StartInfo.FileName = fileName;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();
}
This does successfully start the AutoUpdater.exe but then exists both, the first program and the AutoUpdater.exe, latter should not be exited as that should do the updating.
How to exit only one program from the same solution?
Add the code to shut down the main app in to the updater. You know the process name so it should be fairly easy. This way you can also check that the updater actually starts running before the main app is shut down.
Code below is borrowed from this answer: https://stackoverflow.com/a/49245781/3279876 and I haven't tested it my self.
Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
// now check the modules of the process
foreach (ProcessModule module in process.Modules)
{
if (module.FileName.Equals("MyProcess.exe"))
{
process.Kill();
} else
{
enter code here if process not found
}
}
}
This works with a minimal winforms application in my system:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("notepad.exe");
Application.Exit();
but also this:
ExecuteAsAdmin("notepad.exe");
Application.Exit()

C#: Calling a php script and beeing able to stop it

I am currently working on a C# Program which needs to call a local PHP script and write its output to a file. The problem is, that I need to be able to stop the execution of the script.
First, I tried to call cmd.exe and let cmd write the output to the file which worked fine. But I found out, that killing the cmd process does not stop the php cli.
So I tried to call php directly, redirect its output and write it from the C# code to a file. But here the problem seems to be, that the php cli does not terminate when the script is done. process.WaitForExit() does not return, even when I am sure that the script has been fully executed.
I cannot set a timeout to the WaitForExit(), because depending on the arguments, the script may take 3 minutes or eg. 10 hours.
I do not want to kill just a random php cli, there may be others currently running.
What is the best way to call a local php script from C#, writing its output to a file and beeing able to stop the execution?
Here is my current code:
// Create the process
var process = new System.Diagnostics.Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "php.exe";
// CreateExportScriptArgument returns something like "file.php arg1 arg2 ..."
process.StartInfo.Arguments = CreateExportScriptArgument(code, this.content, this.options);
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
// Start the process or cancel, if the process should not run
if (!this.isRunning) { return; }
this.currentProcess = process;
process.Start();
// Get the output
var output = process.StandardOutput;
// Wait for the process to finish
process.WaitForExit();
this.currentProcess = null;
To kill the process I am using:
// Mark as not running to prevent starting new
this.isRunning = false;
// Kill the process
if (this.currentProcess != null)
{
this.currentProcess.Kill();
}
Thanks for reading!
EDIT
That the cli does not return seems to be not reproducible. When I test a different script (without arguments) it works, probably its the script or the passing of the arguments.
Running my script from cmd works just fine, so the script should not be the problem
EDIT 2
When disabling RedirectStandardOutput, the cli quits. could it be, that I need to read the output, before the process finishes? Or does the process wait, when some kind of buffer is full?
EDIT 3: Problem solved
Thanks to VolkerK, I / we found a solution. The problem was, that WaitForExit() did not get called, when the output is not read (probably due to a full buffer in the standard output). My script wrote much output.
What works for me:
process.Start();
// Get the output
var output = process.StandardOutput;
// Read the input and write to file, live to avoid reading / writing to much at once
using (var file = new StreamWriter("path\\file", false, new UTF8Encoding()))
{
// Read each line
while (!process.HasExited)
{
file.WriteLine(output.ReadLine());
}
// Read the rest
file.Write(output.ReadToEnd());
// flush to file
file.Flush();
}
Since the problem was that the output buffer was full and therefore the php process stalled while waiting to send its output, asynchronously reading the output in the c# program is the solution.
class Program {
protected static /* yeah, yeah, it's only an example */ StringBuilder output;
static void Main(string[] args)
{
// Create the process
var process = new System.Diagnostics.Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "php.exe";
process.StartInfo.Arguments = "-f path\\test.php mu b 0 0 pgsql://user:pass#x.x.x.x:5432/nominatim";
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
output = new StringBuilder();
process.OutputDataReceived += process_OutputDataReceived;
// Start the process
process.Start();
process.BeginOutputReadLine();
// Wait for the process to finish
process.WaitForExit();
Console.WriteLine("test");
// <-- do something with Program.output here -->
Console.ReadKey();
}
static void process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data)) {
// edit: oops the new-line/carriage-return characters are not "in" e.Data.....
// this _might_ be a problem depending on the actual output.
output.Append(e.Data);
output.Append(Environment.NewLine);
}
}
}
see also: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline%28v=vs.110%29.aspx

Start hidden Internet Explorer

my company is using Sharepoint and ADFS. For the use of WebDav however we need the users to get some tokens which they only get by opening the Internet Explorer and navigate to two sites. However they will lose the token every ~30 Minutes, so it has to be a recurring task.
So now my job is to:
Open 2 Websites with IE
Every 30 Minutes
Don't annoy the user
My current solution is "kinda" working but I am not really satisfied with it.
I have only VSExpress so no Services.
I have a minimized max opacity visible false Windows Form.
I have a GPO which copies an EXE file to the computer and then creates a timed job that starts it every 30 minutes after login. However it is not really working out, people still have trouble accessing webdav if they don't run the EXE manually. Also whenever the EXE is running the current application the user is working in loses focus which is kinda annoying when you are typing something and have to click back in.
My current code is looking like this:
private void Form1_Load(object sender, EventArgs e)
{
MainMethod();
}
private void MainMethod()
{
RegistryKey root = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Classes\InternetExplorer.ApplicationMedium\CLSID", false);
if (root!=null)
{
opensite();
Application.Exit();
}
}
private void opensite()
{
try
{
SHDocVw.InternetExplorer _ie1 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
SHDocVw.InternetExplorer _ie2 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
_ie1.Visible = false;
_ie2.Visible = false;
_ie1.Navigate("SITENAME1.html");
_ie2.Navigate("SITENAME2.html");
System.Threading.Thread.Sleep(10000);
_ie1.Quit();
_ie2.Quit();
}
catch(Exception e)
{
}
}
However, I feel there is a much more elegant way to do this. I heard the only way to open a hidden IE is via
(SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
But with this I rely on the registry key which not all clients have.
Can you help me open the IE in a reliable way and maybe have some tipps on how I should set the recurring task to just start every 30 minutes (because I think it is not doing it correctly atm).
Thank you all in advance.
EDIT:
Thanks to https://stackoverflow.com/users/5065008/daniel-waghorn
I now replaced the opensite bit with:
private void Form1_Load(object sender, EventArgs e)
{
MainMethod();
}
private void MainMethod()
{
openProc("SITE1.html");
openProc("SITE2.html");
Application.Exit();
}
private void openProc(string site)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
string ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
startInfo.FileName = ProgramFiles + #"\Internet Explorer\iexplore.exe";
startInfo.Arguments = "" + site + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
Thanks again!
You can use ProcessStartInfo to create a new instance of IE:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ""C:\Program Files\Internet Explorer\iexplore.exe"";
startInfo.Arguments = "" + url + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
You could use Environment.SpecialFolder.ProgramFiles to get the user's Program Files directory path if you don't want to hard-code it.
I must point out that startInfo.WindowStyle will start Internet Explorer hidden although if at any point IE decides to alter that value for any reason it may show.
Ideally if you aren't tied to using Internet Explorer to get the tokens another alternative would be to use the above code but target cURL or something similar. With this it will run in the command line which you can guarantee not to show or steal focus with startInfo.CreateNoWindow.
I think you'll find your answer in one of these links:
Handle IE To filling a form c#
Opening a Hidden Internet Explorer Window without it getting Focus?

Starting process is not always starting

I have the following function:
public static void ExecuteNewProcess(string fileToExecute, Action<string> writeToConsole)
{
ProcessStartInfo startInfo = new ProcessStartInfo(fileToExecute);
Process processToExecute = new Process();
startInfo.UseShellExecute = true;
processToExecute.StartInfo = startInfo;
if (!File.Exists(fileToExecute))
{
throw new FileNotFoundException("File not found for execution");
}
if (processToExecute.Start())
{
Thread.Sleep(6000);
Process[] procs = Process.GetProcesses();
if (IsProcessOpen(Path.GetFileNameWithoutExtension(fileToExecute)))
{
writeToConsole(fileToExecute + " launched successfully...");
}
else
{
writeToConsole(fileToExecute + " started but not found.");
throw new Exception("Application started butnot found running...Delay = 6000, File Name = " + Path.GetFileNameWithoutExtension(fileToExecute));
}
}
else
{
writeToConsole("Error Launching application: " + fileToExecute);
throw new Exception("Application did not launch " + fileToExecute);
}
}
private static bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
So the problem is that sometimes my application that I am trying to start with this function is not starting (It starts about 80% of the time). However I do go through the part of the code that checks to make sure it started and outputs as such. I am not sure why it is not starting. I double click the application when I see it doesnt start to make sure its a valid exe. It always is and starts fine. I have also tried using the shell and not using the shell. No difference.
I am thinking that processToExecute is getting cleaned up before the application has successfully started all the way. Just a guess though.
I appreciate your help in advance.
I put in a few sleeps to see if it was just happening too fast.
The reason 20% of time your application don't show up when started by Process because of the time the application takes load before we see the user Interface
So their are two ways through which you might be able to achieve it
1. start the process - > process.Start(); And then process.WaitForInputIdle();
OR
2. start the process - > process.Start(); And then Thread.Sleep(1000);
//make sure you give reasonable milliseconds

C# Process - Pause or sleep before completion

I have a Process:
Process pr = new Process();
pr.StartInfo.FileName = #"wput.exe";
pr.StartInfo.Arguments = #"C:\Downloads\ ftp://user:dvm#172.29.200.158/Transfer/Updates/";
pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.UseShellExecute = false;
pr.StartInfo.
pr.Start();
string output = pr.StandardOutput.ReadToEnd();
Console.WriteLine("Output:");
Console.WriteLine(output);
Wput is an ftp upload client.
At the moment when I run the process and begin the upload, the app freezes and the console output won't show until the end. I guess the first problem is solvable by using a Thread.
What I want to do is start an upload, have it pause every so often, read whatever output has been generated (use this data do make progress bar etc), and begin again.
What classes/methods should I be looking into?
You can use the OutputDataReceived event to print the output asynchronously. There are a few requirements for this to work:
The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.
An example of this working is below. It's just doing a long running operation that also has some output (findstr /lipsn foo * on C:\ -- look for "foo" in any file on the C drive). The Start and BeginOutputReadLine calls are non-blocking, so you can do other things while the console output from your FTP application rolls in.
If you ever want to stop reading from the console, use the CancelOutputRead/CancelErrorRead methods. Also, in the example below, I'm handling both standard output and error output with a single event handler, but you can separate them and deal with them differently if needed.
using System;
using System.Diagnostics;
namespace AsyncConsoleRead
{
class Program
{
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "findstr.exe";
p.StartInfo.Arguments = "/lipsn foo *";
p.StartInfo.WorkingDirectory = "C:\\";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
static void OnDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
}
}
The best method would be to use libraries which support FTP, instead of relying on external applications. If you don't need much info from the external application and are not verifying their outputs, then go ahead. Else better use FTP client libs.
May be you would like to see libs/documentations:
http://msdn.microsoft.com/en-us/library/ms229711.aspx
http://www.codeproject.com/KB/IP/ftplib.aspx
http://www.c-sharpcorner.com/uploadfile/danglass/ftpclient12062005053849am/ftpclient.aspx

Categories

Resources