Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to stop a program from starting up using c#?

Here's the idea, I'd like to make a service? that will look for a certain program starting up and dissallow it unless certain conditions are met.

Let's say I have a game I want to stop myself from playing during the week. So if I start it up on any day other than Friday/Saturday/Sunday, it will intercept and cancel. Is this possible with C#?

Main thing I am looking for is how to catch a program starting up, rest should be easy.

like image 225
Programmin Tool Avatar asked Sep 28 '08 18:09

Programmin Tool


2 Answers

Well, you can definitely determine which programs are running by looking for the process names you want (GetProcessesByName()) and killing them.

Process[] processes = Process.GetProcessesByName(processName);
foreach(Process process in processes)
{
   process.Kill();
}

You could just have a list of them you didn't want to run, do the time check (or whatever criteria was to be met) and then walk the list. I did something like this once for a test and it works well enough.

like image 130
itsmatt Avatar answered Oct 05 '22 07:10

itsmatt


I don't know about C# in particular here but one why you could accomplish this (I'll be it is a dangerous way) is by using the Image File Execution Options (http://blogs.msdn.com/junfeng/archive/2004/04/28/121871.aspx) in the registry. For whatever executable you are interested in intercepting you could set the Debugger option for it and then create a small application that would be used at the debugger then have it essentially filter these calls. If you wanted to allow it to run then start up the process otherwise do whatever you like. I’ve never attempted this but it seems like it could do what you want.

Or if you wanted to react to the process starting up and then close it down you could use a ProcessWatcher (http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx) and subscribe to the process created event and then close it down if needed. However that is more of reactive approach instead a proactive approach like the first one.

like image 24
Wes Haggard Avatar answered Oct 05 '22 07:10

Wes Haggard