Run through admin rights

(including <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> )

I use the list of processes in List<string> - Processes that do not need to kill.

  var name = new List<string> { "svchost", "csrss", "System", "wininit","ApplicationFrameHost", "irstrtsv","scrncap","lsass","devenv","System","audiodg","fontdrvhost", "wininit","taskmgr","dwm","spoolsv","smss","alg","igfxTray","IpOverUsbSvc", "SearchIndexer","lsm","taskhost","svchost","explorer","scrncap","opera", "winlogon","services","conhost","ctfmon","ati2evxx","BTTray","nvcontainer", "dwm","issch","jusched","rthdcpl","rundll32","wmiprvse","wudfhost","SwiService", "AvrcpService","devenv","msvsmon","LMS","jhi_service","nvvsvc","nvcontainer", "igfxHK","igfxEM","igfxCUIService","IAStorDataMgrSvc","PerfWatson2","PresentationFontCache", "SecurityHealthService","ShellExperienceHost","sihost","SynTPEnh","SynTPEnhService","SynTPHelper", "SystemSettings","taskhostw","ViakaraokeSrv","WinStore.App","WUDFHost", }; try { foreach (var anti in Process.GetProcesses()) { if (name.IndexOf(item: anti.ProcessName.ToLower()) < 0 && anti.ProcessName != Process.GetCurrentProcess().ProcessName) { // Обходим все процессы из List<string> и убиваем все остальные. anti.Kill(); } } } catch (Exception ex) { File.WriteAllText("ErrorEx.txt", contents: ex.Message); } 

After launching, at most one process is killed from the list.

Run on Windows 10.

PS: ErrorEx.txt - Access is denied!

What is he missing :)?

  • @NickProskuryakov, Understood)) - GooliveR
  • And you put the try / catch inside the loop - VladD
  • @VladD, and did so :) - GooliveR

1 answer 1

In the course of the investigative experiment I found out that by working with the processes, not everyone can be accessed. As a result, I moved try\catch inside the foreach and left it empty (to catch all exceptions) - although this is not correct. I think you can fix it (everyone will find what to do for himself)

Decision:

 foreach (var anti in Process.GetProcesses()) { try { if (name.IndexOf(item: anti.ProcessName.ToLower()) < 0 && anti.ProcessName != Process.GetCurrentProcess().ProcessName) { anti.Kill(); } } catch { /*(Exception ex) { File.WriteAllText(path: "ErrorEx.txt", contents: ex.Message); }*/ } }