449

I keep getting this error during the build of my VS2012 C# project

Error   41  Could not copy "obj\Debug\WeinGartner.WeinCad.exe" to
 "bin\Debug\WeinGartner.WeinCad.exe". 
 Exceeded retry count of 10. Failed.    


Error   42  Unable to copy file "obj\Debug\WeinGartner.WeinCad.exe" to
"bin\Debug\WeinGartner.WeinCad.exe". The process cannot access the file
'bin\Debug\WeinGartner.WeinCad.exe' because it is being used by another 
process.    

Now I've figured out that killing the process

Weingartner.WeinCad.vhost.exe

works (sometimes ) but this is getting on my nerves. Any way to stop this happening at all?

My debugger settings are

enter image description here enter image description here

3
  • For me it was caused by manually launched .exe in the Release directory. The problem was that VS cannot copy over an executable that is still running. I'll try to fix it with properly cleaning up resources so the program does not leave hanging after window close button.
    – lahjaton_j
    Commented Jan 8, 2016 at 8:24
  • There is a good summary of this problem with typical steps to resolve in this question
    – LightCC
    Commented Oct 6, 2017 at 17:57
  • This was happening for me because Windows Defender decided it no longer liked the .exe from the VS2019 project upon which I am working. Been working on this for weeks with no issue but today, guess a new update didn't like it. Had to exclude my Source folders. Stopped happening.
    – AdvApp
    Commented Jan 30, 2020 at 21:48

72 Answers 72

547

I have encountered similar error messages in Visual Studio 2013.

Mostly, I have found that this situation has occurred when a debug process was halted because of an exception.

When clean+build has not resolved this problem for me, I have had success by doing the following:

  • Closing Visual Studio
  • Deleting the bin and obj folders, and
  • Reopening Visual Studio.

This "bug" has existed since Visual Studio 2003.

Finally, I have also found that I can often overcome this problem by simply renaming the executable file and then deleting it.

33
  • 80
    This is not a solution, at the best a partial solution. I dont wanna restart VS every 10min. Cleaning the solution works for me, but cleaning it every 10min is also not a solution.
    – Legends
    Commented Apr 15, 2015 at 19:39
  • 8
    From my experience, VS2013 does this at least 10 times a day for me no matter what machine I am developing on. It's like the bug has gotten worse. Just sayin'
    – A.R.
    Commented May 7, 2015 at 21:16
  • 4
    Yeah... issue still exist sin 2017... pretty annoying. Commented Jul 24, 2017 at 4:28
  • 69
    bug still exist in VS 2019.
    – Akash KC
    Commented May 14, 2019 at 14:41
  • 24
    Getting this in VS 2022 as well.
    – ouflak
    Commented Jan 16, 2023 at 10:07
114

In Visual Studio Premium 2013 (Update 3), I solved this with a pre-build one-liner:

(if exist "$(TargetDir)*old.pdb" del "$(TargetDir)*old.pdb") & (if exist "$(TargetDir)*.pdb" ren "$(TargetDir)*.pdb" *.old.pdb)

This gracefully deletes any old PDB files (if it can), then renames anything that's left with a .old.pdb extension. A nice side effect is that if the old PDB is still locked, it just adds another .old piece to the filename, and they all get cleaned up next time you restart Visual Studio and do a build.

For example, build/debug session 1 leaves MyProject.pdb locked.
The next time you build:
MyProject.pdb --> MyProject.old.pdb

Then, build/debug session 2 is started, and both MyProject.pdb and MyProject.old.pdb are still locked:
MyProject.old.pdb --> MyProject.old.old.pdb
MyProject.pdb --> MyProject.old.pdb

Finally, restarting Visual Studio and doing a fresh build will get rid of both of these, and continue the process as usual.

8
  • 5
    The same in VS2010, VS 2012
    – Boogier
    Commented Jan 16, 2015 at 6:26
  • 7
    Thank you, worked perfectly for me with modifying your example to using exe files instead. I think this might be a bug in the latest VS 2015 CTP as well. Commented Feb 22, 2015 at 20:55
  • 3
    I hate having to do this on principal, but it works, so there's that! :) Thanks for sharing this pearl, Geoff! Commented Mar 20, 2015 at 18:27
  • 2
    Latest (2018-03-11) Visual Studio 2017 v15.6.1: still an issue. Debugging, exception, assemblies in the target directory locked. The above solution with *.pdb changed to *.dll still applies. Commented Mar 11, 2018 at 9:30
  • 2
    To save some a little time... To add this, right click on your project name, select "Properties" Expand "Build" Select "Events" and enter this into the "Pre-build event" textbox.
    – Danimal111
    Commented Apr 19, 2023 at 12:35
110

It's because you have closed your application, but it's still running in background.

Temporary solution:

  • Go to Task Manager (Ctrl + Shift + Esc).
  • Go to Processes tab and find "YourProjectName.exe".
  • Check "Show processes from all users" if you can't find your process.
  • End Process it.

Permanent solution: you have to close your application through coding. Here is the code...

System.Windows.Forms.Application.Exit();

You have to put this code in to the form's closing event in all form. Example:

private void frm_menu_FormClosing(object sender, FormClosingEventArgs e)
{
    System.Windows.Forms.Application.Exit();
}
5
  • 2
    This was exactly it. Visual Studio had crashed and IIS Express was still running (in my case). All I had to do was open the task bar and right-click the IIS Express icon and exit. Thank you. Commented Jul 25, 2017 at 3:16
  • This worked for me; I couldn't delete the obj and bin folders as another process was using them. Thankfully Windows 10 actually said what the name of it was; once it was closed in Task Manager the problems went away
    – Novastorm
    Commented Mar 23, 2018 at 14:47
  • On my side it was on the User tab. Also, I found it using the Process Explorer Commented Nov 11, 2020 at 5:42
  • Logged off server... logged back on... Issue GONE! Thanks for this permanent fix also... My trouble was killing the process over and over while debugging due to issues...
    – Danimal111
    Commented May 26, 2022 at 23:08
  • Worked for me. I ended the background process and rebuild. Commented May 15, 2023 at 4:32
28

I have solved it by killing IISExpress in task manager

0
25

the .vhost.exe is a debugger process, so it appears that the process being debugged hasn't closed properly. Chances are you have a bug that's keeping it alive and are not stopping the debug process correctly - there are options to detach from the process when you click 'stop debugging' instead of actually killing the debugger so maybe you have that set.

But that's the problem - the file you're trying to copy over is locked (ie still being used) by the OS so its preventing the copy. Ensure that file is free and you'll be able to copy.

2
  • I've added my debugger options to the questions. I'm pretty sure it should be killing the process but maybe I don't understand some options. Commented Aug 7, 2013 at 12:17
  • In Visual Studio 2019, I am getting a similar message, although it now mentions the process in some of the output (not all). It was testhost.x86.exe that I had to kill off via Task Manager. After that it did seem to stop detecting one of the test processes.
    – Andez
    Commented Feb 14, 2020 at 19:00
22

The problem is that the debugger/builder creates the executable or library that is identified as a threat by the Anti-virus and therefore deleted right before it could be executed.

Preferably you should tweak your Anti-Virus software to not analyse your project folder.


Some ways to do this, ranked from best to worse, are:

  1. Excluding your project folder from the Anti-Virus
  2. Excluding pdb files (Not a 100% guaranteed fix)
  3. Turning off Real-Time protection (Not Recommended)
5
  • Good catch. I forever hate Avast. Commented Aug 14, 2014 at 23:49
  • Avast was the problem for me too. Disabling the File System Shield was the answer. I tried adding my Visual Studio\Projects folder to the Exclusions but that didn't work.
    – KeithB
    Commented Dec 9, 2014 at 13:03
  • 1
    I have the same problem with Symantec Endpoint protection. Someone in the IT department has ratcheted up the security level pretty high :-) Thanks Pitrs.
    – ssimm
    Commented Aug 24, 2016 at 14:56
  • I will add, that you can create exception for the obj\Debug directory for convinient use, instead of disabling the AV or one of it's protective tools.
    – A. Kali
    Commented Feb 15, 2017 at 11:07
  • Thanks! I found that it was an MalwareBytes blocking my .exe file.
    – NL3294
    Commented Mar 15, 2017 at 23:22
17

I was able to fix this issue (VS 2010) through supplying following pre build action;

if exist "$(TargetPath).locked" del "$(TargetPath).locked"

if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
1
  • 1
    @luckyluke, In your project properties, there is section where you can add pre-Build script. Copy and past the above script in that designated area and rebuild the project / run your application
    – S.N
    Commented Sep 21, 2017 at 11:26
14

Quote:

A workaround is to put this in the Pre-build event command line property of the >project (In the build Events tab):

Code Snippet

if exist "$(TargetPath).locked" del "$(TargetPath).locked"

if exist "$(TargetPath)" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
0
11

My 10 cents contribution.

I still have this problem occasionally on VS 2015 Update 2.

I found that switching compilation target solves the problem.

Try this: if you are in DEBUG switch to RELEASE and build, then back to DEBUG. The problem is gone.

Stefano

3
  • Yeah! That's it. This is a simple solution to this annoying problem! Totally worked for me. Easy and quick! Thanks alot. Commented Jan 24, 2017 at 6:35
  • 2
    It works for me! Hint: With deactivated Debug>>Options>>Debugging>>General>>"Use Managed Compatibility Mode" the workaround is not required!
    – leon22
    Commented Aug 6, 2018 at 8:47
  • The problem still exists in VS2022! Valuable 10cts from Stefano! Thanks! Much quicker for a larger project than closing and re-opening VS, which works also.
    – josh
    Commented May 14, 2022 at 11:25
10

Exception

In some cases in Visual Studio when you (Build || Rebuild) on top of running IISExpress you faced with this Exception:

Unable to copy file "obj\Debug\YourProjectName.dll" to bin\YourProjectName.dll". the process cannot access the file 'bin\YourProjectName.dll' because it is being used by another process

Solution

  1. Right click on web project that needs to build.
  2. Click on properties.
  3. Select Build Events Tab on the left side.
  4. In Pre-build events command line paste these 2 line:
tasklist /fi "imagename eq iisexpress.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "iisexpress.exe"

You are good 2 GO!

10

Follow the below steps

  1. Open Task Manager ( Ctrl + Alt + Delete )
  2. Under Performance tab select select <ProjectNameOfYours.exe>.
  3. Click on End Process.
  4. Now Build solution.

Above steps resolved error permanently :)

9

Killing the process w3wp.exe (IIS) will often solve this.
Generally, you can know the process that has the lock on the file by navigating to the bin folder and trying to delete it. The error message that will pop up, in case another process is using it, will contain the name of the process that needs to be killed.

Using Blazor and Fluent UI Web Components I got the errors below on publish:

https://learn.microsoft.com/en-us/fluent-ui/web-components/integrations/blazor

C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk.Razor\targets\Microsoft.NET.Sdk.Razor.StaticWebAssets.targets(615,5): Warning MSB3026: Could not copy "C:\Users\oscar.nuget\packages\microsoft.fast.components.fluentui\1.5.3\staticwebassets\icons\TextDirectionRotate270Right\en\text_direction_rotate_270_right_24_regular.svg" to "C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\MyVeryLongProjectName12345678\Server\obj\Release\net6.0\PubTmp\Out\wwwroot_content\Microsoft.Fast.Components.FluentUI\icons\TextDirectionRotate270Right\en\text_direction_rotate_270_right_24_regular.svg". Beginning retry 10 in 1000ms. Could not find a part of the path 'C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\MyVeryLongProjectName12345678\Server\obj\Release\net6.0\PubTmp\Out\wwwroot_content\Microsoft.Fast.Components.FluentUI\icons\TextDirectionRotate270Right\en\text_direction_rotate_270_right_24_regular.svg'. C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk.Razor\targets\Microsoft.NET.Sdk.Razor.StaticWebAssets.targets(615,5): Warning MSB3026: Could not copy "C:\Users\oscar.nuget\packages\microsoft.fast.components.fluentui\1.5.3\staticwebassets\icons\TextDirectionRotate270Right\en\text_direction_rotate_270_right_20_regular.svg" to "C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\MyVeryLongProjectName12345678\Server\obj\Release\net6.0\PubTmp\Out\wwwroot_content\Microsoft.Fast.Components.FluentUI\icons\TextDirectionRotate270Right\en\text_direction_rotate_270_right_20_regular.svg". Beginning retry 10 in 1000ms. Could not find a part of the path 'C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\MyVeryLongProjectName12345678\Server\obj\Release\net6.0\PubTmp\Out\wwwroot_content\Microsoft.Fast.Components.FluentUI\icons\TextDirectionRotate270Right\en\text_direction_rotate_270_right_20_regular.svg'. C:\Program Files\dotnet\sdk\6.0.402\Sdks\Microsoft.NET.Sdk.Razor\targets\Microsoft.NET.Sdk.Razor.StaticWebAssets.targets(615,5): Error MSB3027: Could not copy "C:\Users\oscar.nuget\packages\microsoft.fast.components.fluentui\1.5.3\staticwebassets\icons\TextDirectionHorizontalRight\ko\text_direction_horizontal_right_24_regular.svg" to "C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\MyVeryLongProjectName12345678\Server\obj\Release\net6.0\PubTmp\Out\wwwroot_content\Microsoft.Fast.Components.FluentUI\icons\TextDirectionHorizontalRight\ko\text_direction_horizontal_right_24_regular.svg". Exceeded retry count of 10. Failed.

Looking at

C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\MyVeryLongProjectName12345678\Server\obj\Release\net6.0\PubTmp\Out\wwwroot\_content\Microsoft.Fast.Components.FluentUI\icons\TextDirectionHorizontalRight\ko\text_direction_horizontal_right_24_regular.svg

It is 261 characters long.

I then tried to Enable Long Paths and reboot my computer.

https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry

This did not work and I got the same error. I then moved my project to C:\MyVeryLongProjectName12345678\ instead of C:\Users\oscar\source\repos\MyVeryLongProjectName12345678\. After doing this everything worked out.

2
  • Curiously, I ran into this even though my path was only 161 characters. The setting in the registry definitely had the same default value. So it's very strange that enabling Long Path names actually worked to resolve this. I think there must have been a bit more going on. Also, I didn't have to reboot my computer after changing the setting. That was unexpected.
    – ouflak
    Commented Jan 18, 2023 at 13:13
  • +1 Only this solution worked for me. I had taken checkout of a new branch on my local. And one of the .sql embedded resource was using long file name. Trimming the file name fixed the issue.
    – nraina
    Commented Jan 31, 2023 at 10:50
7

It seems that by change the assembly name of a project fixes the problem.

So instead of this

enter image description here

I change it to this

enter image description here

Notice that I just changed it from Increment and Recall to Increment_Recall, I just removed the spaces. It is now working fine to me.

0
6

I think I solved it removing the check mark to Break all processes when one process breaks in Debug options (op's first screenshot->second option).
It's been building/running well for a while since I unchecked it.
I'm using MySql NET Connector and DevExpress controls in my project. May be one of them was not disposing connections, bindings, etc. well because of this flag beeing activated.

EDITED: definitely it works! No more 'Unable to copy file' and no more Form designer errors.

2
  • 1
    None of the other solutions worked for me. This is the only one. I´m using Visual Studio 2017 13.2
    – xleon
    Commented May 16, 2017 at 16:26
  • This works always. I use VS2013 Community Edition. Here is my Solution with 17 projects: qurancode.com
    – Ali Adams
    Commented Nov 29, 2020 at 6:48
6

If none of the answers works, try this simple check. Find for any MSbuild.exe running and holding your project EXE. Kill MSBuild.exe and you should be good to go.

4

I faced the same problem on VS 2012 Version 11.0.60610.01 Update 3 on Windows 8

There were no designer windows open and the project was a simple console application.

The removal of the vshost process accessing the file does not work most of the time since the process isn't accessing the file.

The simplest workaround that works and takes the least amount of time is to remove the project from the solution, build another project in the solution and then add the original back.

It's an irritant and waste of time but it's the least expensive of all the other options that I know of.

Hope this helps...

2
  • All you have to do is Rebuild All and everything's fine for another 10 tries. Not much of an inconvenience. Commented Jun 24, 2015 at 15:36
  • @Scott Shaw-Smith Doesn't work for me. And based on some of the other comments I've seen, it doesn't work for others either. In my case uninstalling Avast fixed it.
    – user316117
    Commented Aug 5, 2015 at 15:02
4

Add in pre-build event of your master project taskkill /f /fi "pid gt 0" /im "YourProcess.vshost.exe"

2
  • Do not really like to solve the problem this way, but this worked!
    – Petter T
    Commented Jan 11, 2016 at 11:35
  • I've found this the simplest working solution for the problem.
    – dscharge
    Commented Jun 15, 2016 at 11:22
3

I cannot give a solution to prevent this from happening but you can at least RENAME the locked file (windows explorer, or classic command window) and then compile/build. No need to reboot or restart VS201x. With some experience you can add a pre-build script to delete old files or rename then out-of-the-way in case there's a lock.

3

See this other answer. Basically, you could have MSBuild.exe processes running in the background consuming resource files. If you have any pre or post build tasks that cause an MSBuild to be kicked off via command line, try adding the "/nr:false" flag to this command. But again, see the previous answer for more specific details.

2
  • Snap, i have the same problem in VS2015 update 2 - MSBuild,exe process needs to be killed in TaskManager before I can re-build. Commented Jun 2, 2016 at 9:09
  • The article link in Josh's answer above suggests using a system environment variable to disable node reuse within Visual Studio and the MSBuild process (MSBUILDDISABLENODEREUSE=1) - this worked for me. Commented Jun 2, 2016 at 9:30
3

Here is a script to definitely get rid of this issue:

REM   This script is invoked before compiling an assembly, and if the target file exist, it moves it to a temporary location
REM   The file-move works even if the existing assembly file is currently locked-by/in-use-in any process.
REM   This way we can be sure that the compilation won't end up claiming the assembly cannot be erased!

echo PreBuildEvents 
echo  $(TargetPath) is %1
echo  $(TargetFileName) is %2 
echo  $(TargetDir) is %3   
echo  $(TargetName) is %4

set dir=C:\temp\LockedAssemblies

if not exist %dir% (mkdir %dir%)

REM   delete all assemblies moved not really locked by a process
del "%dir%\*" /q

REM   assembly file (.exe / .dll) - .pdb file and eventually .xml file (documentation) are concerned
REM   use %random% to let coexists several process that hold several versions of locked assemblies
if exist "%1"  move "%1" "%dir%\%2.locked.%random%"
if exist "%3%4.pdb" move "%3%4.pdb" "%dir%\%4.pdb.locked%random%"
if exist "%3%4.xml.locked" del "%dir%\%4.xml.locked%random%"

REM Code with Macros
REM   if exist "$(TargetPath)"  move "$(TargetPath)" "C:\temp\LockedAssemblies\$(TargetFileName).locked.%random%"
REM   if exist "$(TargetDir)$(TargetName).pdb" move "C:\temp\LockedAssemblies\$(TargetName).pdb" "$(TargetDir)$(TargetName).pdb.locked%random%"
REM   if exist "$(TargetDir)$(TargetName).xml.locked" del "C:\temp\LockedAssemblies\$(TargetName).xml.locked%random%"

REM PreBuildEvent code
REM   $(SolutionDir)\BuildProcess\PreBuildEvents.bat  "$(TargetPath)"  "$(TargetFileName)"  "$(TargetDir)"  "$(TargetName)"

REM References:
REM   http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
REM   http://stackoverflow.com/a/2738456/27194
REM   http://stackoverflow.com/a/35800302/27194

The script needs to be invoked from each VS project pre build event.

$(SolutionDir)\BuildProcess\PreBuildEvents.bat  "$(TargetPath)"  "$(TargetFileName)"  "$(TargetDir)"  "$(TargetName)"

enter image description here

3
  • 1
    This is the solution that worked for me (that includes the .bat file and the call to the .bat file from the pre-build event). I am using VS2022, we are in Sep 2023. Commented Sep 4, 2023 at 18:40
  • 1
    Glad it help, this solution still works fine on our end too after years of relying on it :) Commented Sep 6, 2023 at 7:20
  • 1
    I am a DBA and C# for me is just a hobby for my own personal projects, there is no way I would come out with this level of secure, reliable and resilient solution, that once you put it in place you forget about it. Very thankful to you that you took the time to post it here in its entirety. Commented Sep 7, 2023 at 10:26
3
  1. Open project properties [ menu > project > properties ]
  2. Choose "debug" tab
  3. Uncheck "Enable the visual studio hosting process"
  4. Start debugging [F5]
  5. You will receive security warning , just "ok". Lets application running
  6. Stop debugging.
  7. Check option "Enable the visual studio hosting process" , under debug tab,
  8. Now , try to start debugging , you will not see error again

[Work for me]

3
  • Why was this at -2? It has worked for me as well. It makes zero sense but hey, if it works, it works.
    – Wakka02
    Commented Dec 2, 2016 at 8:03
  • Is this a permanent solution? ie do you have to do these 8 steps every time? Commented Apr 10, 2017 at 16:19
  • vs17 does not have the hosting process option Commented Mar 9, 2018 at 12:02
2

I finally how fix it. Why we can't continue debug after the first debug because the first debug exe still running. So that, after first debug, you need to go to Task Manager -> Process Tab -> [your project name exe] end the exe process.

it works for me :)

1
  • Wow, Thanks man, exactly my problem. As it asks user password for me while running exe, first time it didn't fire. When i try to delete that app in process list and then debug again, it worked flawlessly. Commented Mar 3, 2020 at 15:44
2

I didn't realize I still had my debugger attached and was trying to build in the same Visual Studio instance. Once I stopped the debugger I was able to build.

2

@Geoff's (https://stackoverflow.com/a/25251766/3739540) answer is good, but it throws error code 1 on recompile.

Here is what worked for me (2>nul 1>nul on the end + exit 0):

(if exist "$(TargetDir)*old.pdb" del "$(TargetDir)*old.pdb") & (if exist "$(TargetDir)*.pdb" ren "$(TargetDir)*.pdb" *.old.pdb) 2>nul 1>nul
(if exist "$(TargetDir)*old.dll" del "$(TargetDir)*old.dll") & (if exist "$(TargetDir)*.dll" ren "$(TargetDir)*.dll" *.old.dll) 2>nul 1>nul
exit 0
2

I have noticed some answers that solved my problem, BUT, just in case anyone is having the same problem I was.

IF YOU ARE RUNNING A CONSOLE APP: BEFORE YOU DO ANYTHING ELSE.

Make sure you have closed out any console windows that may have been opened from a previous build. For instance I was just testing some code in a console application, I didn't realize that the console window from one of the previous times I ran my program was open. During that session I was debugging, the window got pushed to the back and I couldn't see it. Just saying, this could be your problem, so check to make sure that is not the problem.

2

If you are debugging T4 templates, then this happens all the time. My solution (before MS fixes this) would be just to kill this process:

Task Manager --> User --> T4VSHostProcess.exe

This process only comes up when you debug a T4 template, not when you run one.

2

I found a complete solution!

Most answers tell you to kill the process, however with process hacker, I couldn't find any.

I found a relatively simple solution.

  1. Select your primary form in the Form Designer.
  2. Click to your events tab on the properties menu.
  3. Doubleclick the event FormClosing. This automatically generates the event system and function:

private void [your form name here]_FormClosing(object sender, FormClosingEventArgs e)

  1. Inside this function, add Application.Exit

Like so:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}

helpful image

enter image description here

enter image description here

I hope this helps! This problem really sucked!

Fix 2

Turn on a service called 'Application Experience.'

2
  • 1
    This existed for u because your code was not properly closing the form. This is not a complete solution. There is a mistake in your code keeping the windows forms application and you added code to kill your app Commented Mar 9, 2018 at 12:01
  • Oh, good reminder. The problem came back following a restart of Visual Studio. The thing that fixed it finally turned out to be enabling a service called 'Application Experience.'
    – Kelvin
    Commented Mar 9, 2018 at 14:07
2

You must kill the process after closing. I fixed it with;

Process.GetCurrentProcess().Kill();

Add this code to closing event or method whats you use for closing.

2

If you're running into this issue using Visual Studio Code (vsCode) and/or you're sticking to terminal commands, the clean command will delete the build files and typically fix the issue described:

dotnet clean

Then, you can return to running:

dotnet build

or

dotnet run
2

You can kill the application by script.

If your application is named myapp.exe, run the next script in the terminal:

taskkill /IM myapp.exe /F

the parameters are:

/IM   application name (imagename)

 /F                     Specifies to forcefully terminate the process(es).

Not the answer you're looking for? Browse other questions tagged or ask your own question.