« Why Do We Have So Many Screwdrivers? | ^ Main | Where my Flex skills met my Flash skills »
Restarter.exe 1.0
There was a need to restart an app from another app. So App 1 needs to be restarted by App 2. App 2 was initially built with Zinc but there is reason to believe it isn’t working (hence the fact 12+ App 1’s have been seen open at 1 time). The client suggested a C++/C# version of the restarter so I took off to write one real quick.
Restarter takes 2 params. 1st is app to close and 2nd is app to start. 2nd can be a filename of any sort really. 1st param is the process name and all instances of that name will be closed. It is super simple but very effective.
Check out the code:
using System;
using System.Diagnostics;
namespace Restarter
{
class Program
{
static void Main(string[] args)
{
if(args.Length 1 && args[0] "help")
{
Console.WriteLine("");
Console.WriteLine("Restarter 1.0 Help");
Console.WriteLine("Application Usage: Restarter ApplicationToStop ApplicationToStart.extension");
Console.WriteLine("");
Console.WriteLine("Example - Restarter calc C:\\Windows\\System32\\calc.exe");
}
else if(args.Length < 2)
{
Console.WriteLine("Please provide the application name you would like to stop and the application name you would like to start.");
Console.ReadLine();
}
else
{
Console.WriteLine("Attempting to restart " + args[0]);
stopApp(args[0]);
startApp(args[1]);
}
}
static void stopApp(string appName)
{
Console.WriteLine("Searching for " + appName);
Process[] app = Process.GetProcessesByName(appName);
if(app.Length == 0)
{
Console.WriteLine(appName + " is not open.");
}else
{
Console.WriteLine("There are " + app.Length + " instances of " + appName);
foreach(Process i in app)
{
Console.WriteLine("Closing " + i.ToString());
i.CloseMainWindow();
}
}
}
static void startApp(string appName)
{
Console.WriteLine("Starting " + appName);
Process.Start(appName);
}
}
}
Posted by John C. Bland II on May 3, 2006 3:01 PM | Permalink
TrackBack
TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/124



