3

i need to close all open applications in windows while my application closes how to i do that using c#

i am implementing login/logoff functionality in my application itself instead of windows login/logoff so that i need to close all applications while logoff clicked in my application and pass control to my application after that

Suriyan Suresh
  • 2,964
  • 14
  • 51
  • 80
  • 6
    Remind me not to run that... – Marc Gravell Apr 19 '10 at 05:14
  • You want to kill all running processes? – Ando Apr 19 '10 at 05:18
  • 1
    **Don't do this!** What are you actually trying to do? Is this some kind of kiosk application? You can achieve do kiosk applications via modifications to Group Policy (etc). If you tell us what you actually need this *for*, then we can perhaps we can give you a better alternative... – Dean Harding Apr 19 '10 at 05:32
  • i am implementing login/logoff functionality in my application itself instead of windows login/logoff so that i need to close all applications and pass control to my application – Suriyan Suresh Apr 19 '10 at 05:39
  • 1
    Well, that answer does give us no more information about why you think you have to close all applications by yourself in the logoff scenario. And for what purpose are you trying to do this? If you provide more details, you will probably get an answer for a better approach. – Doc Brown Apr 19 '10 at 06:17
  • 1
    If you actually want to have your own login/logout code running I think maybe you'd be better of looking at GINA and/or "Credential Providers" depending on OS. I can't see how you could write it as an application without making it easy for someone to "break in". – Hans Olsson Apr 19 '10 at 08:41
  • is there a way to get list of applications showing in taskbar – Suriyan Suresh Apr 20 '10 at 06:10

3 Answers3

0
using System.Diagnostics;

Process [] localAll = Process.GetProcesses();
foreach(Process p in localAll)
{
    p.Kill();
}

But you'd probably have to put in some kind of filter to avoid killing certain processes since otherwise the machine might get a bit upset.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
0

For whatever reason you want to do this. This is a bit funnier way to do it!

using System.Diagnostics;

Parallel.ForEach(Process.GetProcesses(), process => process.Kill());

Don't run this with an elevated process though; will probably caus your OS to crash.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
0

You definitely do NOT want to just loop over the system processes and kill them all. There are plenty of things running that should not be killed. If you 'log out' in windows then log in under another user, there are a lot of processes that stay running at the system level and are shared by all logins.

As a good example, if I look in Windows Task Manager at the processes that my current user owns (not system), then I have ATI Catalyst Control running, the Synaptics app for my touchpad, a Logitech app for my mouse, something fo the Bluetooth stack, something for the Audio system, a Dell wireless lan interface management thing... If I kill all those, I suspect my peripheral devices will start behaving strangely, even if another user logs in after me.


Could you just call the actual Windows LogOut stuff with P/Invoke, as suggested here? That way Windows itself can do... well, whatever it does when a user logs out, instead of you having to deal with it yourself.

At worst, I would try to collect up the things that were started by Windows during startup. The 'Statup' items from the StartMenu:

C:\Users\User_Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

and the items from the registry at:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Then loop over the running processes and see which really belong to this user and only stop those.

Community
  • 1
  • 1
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138