How to restart Explorer as a pre-build event in Visual Studio

179 Views Asked by At

I'd like to restart Explorer when building my DLL in Visual Studio. I've tried adding taskkill /f /IM explorer.exe and start explorer as pre-build events in the project configuration. When I then build my project, Explorer will in fact be terminated, but it will not be successfully restarted. The taskbar will remain missing. Only after starting explorer from a command prompt will it return. I've also tried executing a call to those commands in a .bat file, but I get the same problem (although the .bat file works properly when executed on its own, outside Visual Studio). I've also tried putting the relaunch part in a post-build event, but no dice. Is there any trick to getting Explorer to relaunch successfully in a pre-build event? Thanks for any input.

1

There are 1 best solutions below

0
Cody Gray - on strike On

You can restart Explorer by putting the following commands into a Batch file:

@echo off
taskkill /f /im explorer.exe
start explorer.exe
exit

(The /f switch will force the termination of Explorer.)

Then, you can execute that Batch file as a pre-build step (Build Events → Pre-Build Event → Command Line).

If this is a necessary part of your build process, I would recommend adding this Batch file to your solution directory, committing it to your version-control system, and using a relative path in your solution properties.