Narek Malkhasyan
Narek Malkhasyan

Reputation: 1442

How to make Visual Studio close running process before performing build?

I'm building WinForms application using Visual Studio 2010. Every time I make a change in the code I have to run the application and check how it works. The problem is that I do this frequently and once I forget to close the previous instance of the application the compiler generates error "The process cannot access the file bin\Debug....". Is it possible to make Visual Studio close the running instance before performing build?

Upvotes: 17

Views: 10768

Answers (5)

Gunnar Nilsson
Gunnar Nilsson

Reputation: 1

This works if $(TargetFileName) is a DLL.

taskkill /f /fi "imagename eq $(ProjectName).exe"

Upvotes: 0

Meg-90
Meg-90

Reputation: 259

Following code from another stackexchange answer worked for me.

wmic process where name='chromedriver.exe' delete

Upvotes: 4

Bitterblue
Bitterblue

Reputation: 14085

In addition to Morten's answer:

Use taskkill and then ignore errors.

taskkill /F /IM "$(TargetFileName)"
exit 0

Upvotes: 9

tpower
tpower

Reputation: 56886

Add the following to the projects Pre-build event (based on the accepted answer):

taskkill /f /fi "imagename eq $(TargetFileName)"

The command as used in the other answer may result in an error in cases where the process is not running.

This variation uses a filter (/fi) which does not 'fail' even if there is 0 matches.

Upvotes: 33

Morten Frederiksen
Morten Frederiksen

Reputation: 5165

An idea: Make a pre-build step for the executable project that uses Taskkill to kill the process. Read more about Taskkill here: http://technet.microsoft.com/en-us/library/bb491009.aspx

Upvotes: 7

Related Questions