2007-08-01

Open a application as simple as I can

Looking for a launcher application, i found a simple code to open a Notepad® to create a text file.
Almost we can control the Process to close after a time (3 seconds), If you need to save, you will prompt to save o cancel (but only 3 seconds again to respond, later you was forced to exit).


using System;
using System.Diagnostics;
namespace MyNamespace
{
public class MyClass
{
public static void Main()
{
Process p = Process.Start("Notepad.exe");
// Wait for 3 seconds
p.WaitForExit(3000);
if(!p.HasExited)
{
//Force to close el Notepad (if you write a text, you will prompt to save the document)
bool close = p.CloseMainWindow();
// Wait for 3 seconds again
p.WaitForExit(3000);
// The last try to close (if you don't save o discard changes when you was prompted)
if(!p.HasExited)
{
p.Kill();
}
}
p.Dispose();
}
}
}