Reputation: 43
So I'm writing an app that needs to end explorer.exe before it installs. However, when using the following code Windows automatically restarts the process:
Dim proc() = System.Diagnostics.Process.GetProcessesByName("explorer.exe")
For Each item as Process in proc()
item.Kill()
Next
Due to this problem I found a way to kill explorer.exe using taskkill here's the code and it works perfectly fine:
Dim taskkill as New ProcessStartInfo
taskkill.FileName = "cmd.exe"
taskkill.Arguments = "/c taskkill /F /IM explorer.exe"
taskkill.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(taskkill)
But I don't want to depend on cmd.exe to do that task? Can somebody tell me how to do this using vb.net or c# code?
Thanks.
Upvotes: 3
Views: 1680
Reputation: 27
In vb.net to kill explorer.exe you can use
Try
Dim Processes() As Process = Process.GetProcessesByName("explorer")
For Each Process As Process In Processes
Process.Kill()
Next
Catch ex As Exception
End Try
This will only work if you put the code in a timer and starting the timer when you would like to kill explorer.exe.
In c# try make sure you also put it in a timer and start it when you would like to kill explorer.exe
try {
Process[] Processes = Process.GetProcessesByName("explorer");
foreach (Process Process in Processes) {
Process.Kill();
}
} catch (Exception ex) {
}
Hope this helps.
Upvotes: 1
Reputation: 1618
this may be not a good practice of posting others answers,so please forgive me,i just meant to guide you by providing a small light to your problem. this answer is actually from superuser provided by t3hn00b..All credits to him
to start with ,the windows (windows 7 and XP) use a registry key to automatically restart the explorer process.so to disable we have to programatically reset the value of that key,we can use the code.
Dim key As Microsoft.Win32.Registry
Dim ourkey As Microsoft.Win32.RegistryKey
ourkey = key.LocalMachine
ourkey = ourkey.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
ourkey.SetValue("AutoRestartShell", 0)
' Kill the explorer by the way you've post and do your other work
ourKey.SetValue("AutoRestartShell", 1)
or in C#
RegistryKey ourKey = Registry.LocalMachine;
ourKey = ourKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
ourKey.SetValue("AutoRestartShell", 0);
// Kill the explorer by the way you've post and do your other work
ourKey.SetValue("AutoRestartShell", 1)
Anyway i dont recommend changing windows default settings for a problem which have alternatives(using cmd.exe).
the code will have errors,forgive me for that too.just tried to give your problem a little start.try and check it,it is proved to work well in win7 and XP. you can see more details in the superuser link above.Hope it will help.Thanks to the t3hn00b.
Upvotes: 3