1

So i'm trying to write a program that will list all the installed programs on Windows and let user decided which one to be blocked(Killed).

I found the solution of List Installed program here. Get installed applications in a system

And to kill a program you just need the .exe name and kill all the process with that name.

But my problem is the names in the list of installed program is not necccesary the .exe name, and you couldn't find a process without the .exe name. For example, the installed program name is "Google Chrome" but the .exe or the process name would be chrome.

The current solution I can think of is to find out the installed directory of the installed program, find all the .exe files under it. And kill all of them if required. But I don't know how to get the directory of all the installed programs.

So, I can't help wonder, is there a more elegant of doing it. Any suggestions would be appreciated.

Community
  • 1
  • 1
libra
  • 673
  • 1
  • 10
  • 30
  • Get isntalled programs or programs running? Kill means uninstall or "exit" the program? – Tatranskymedved Mar 22 '17 at 08:17
  • installed program, and kill means kill/block/force-exit – libra Mar 22 '17 at 08:17
  • Why would You need to have installed programs, when we are talking about quiting the running ones? =) – Tatranskymedved Mar 22 '17 at 08:19
  • 2
    Windows already has a built in mechanism to enforce software policies via group policy. Why are you rebuilding this? – Damien_The_Unbeliever Mar 22 '17 at 08:20
  • @Damien_The_Unbeliever If it already exist is it something that I could call from my C# program? I need to do this because it's a feature in my program. – libra Mar 22 '17 at 08:23
  • @Tatranskymedved I'm building a self-restraint app, i want user to be able to block Steam for example, for the next 2 hours. – libra Mar 22 '17 at 08:24
  • You can not find the exe location because it is not (always) available in the uninstall registry. I'm not sure if this is possible, because every `.exe` that is on your system can be considered an installed program. You can scan your system for all `exe`s, but what if you have Java installed? Then every `.jar` is also an "installed program". Or what about Python? – Ivar Mar 22 '17 at 08:28

1 Answers1

1

You have to split this question on 2 parts:

1. Find all the programs

What does it mean? It means that You want to find all the executable files (*.exe) on the hard drive of the client. This executable files does include the code which will run in the memory and do some work.

BUT: The code located on harddisk cannot be "killed", as the code (exe file) will start first new instance of application (via OS interface). Once You got the instance we can start talking about other part of question.

Also as stated there, You are unable to get easily 1 exe file, as multiple application might have multiple exe files. And if You really want to have them, You got to run analysis on the harddisk, like there:

var allExePaths =
    from drive in Environment.GetLogicalDrives()
    from exePath in Directory.GetFiles(drive, "*.exe", SearchOption.AllDirectories)
    select exePath;

Also as Ivar stated in comments, there are multiple executables, like *.jar, *.pyc, ... which might be run via other executable file.

2. Kill the required program

If You want to kill some program/application, You need to get it's instance. Instance are distinguished by PID (process ID). Once You got this of the instance, You can kill it. Answer for this is there: How do I kill a process using Vb.NET or C#?

The code below will take all process with particular name and kill them all.

Code:

foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("winword") )
{
    try
    {
        p.Kill();
        p.WaitForExit(); // possibly with a timeout
    }
    catch ( Win32Exception winException )
    {
        // process was terminating or can't be terminated - deal with it
    }
    catch ( InvalidOperationException invalidException )
    {
        // process has already exited - might be able to let this one go
    }
}
Community
  • 1
  • 1
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • Just one quick question, would .jar and .pyc etc executables have their file name same as the process name? – libra Mar 22 '17 at 09:55
  • TBH not sure, I think there would be the running main process (java.exe) and Your app name as a 2nd process (HelloWorld) - I cannot confirm as I'm not able to test it now. – Tatranskymedved Mar 22 '17 at 10:40