In this post I'm starting a new category on my blog: Tips. It is very important to take time to setup your development environment so you can perform tasks as quickly as possible. Here is one hack I have that fixes an occasional problem I have with Visual Studio (VS).
VS has a Build, Rebuild, and Clean Solution options (not to mention Hot Reload). Build (Ctrl+Shift+B) is the quickest. It only builds where the source code has changed. Rebuild forces a full rebuild of the whole solution, regardless of what source code has changed. Clean solution deletes all the files in the bin
folder. The Rebuild implicitly does a clean before it builds.
All of these build your project or solution with varying degrees of reliance on a cache. Visual Studio optimizes the build process by caching some parts of the build process in an obj
folder, before coping the results to the bin
folder.
The problem is caching is hard. Sometimes the obj
folder can get out of sync. This can happen if switching git branches, or if a lot of NuGet packages are changing. I'm not going to prove this claim, but I have been burned enough times that I just can trust this rebuild process.
Even though it is a bit slower, I often do a Rebuild instead of Build. I just trust it more. Not 100%, but more.
Add a keyboard shortcut for Rebuild. VS default key bindings have Build bound to Ctrl+Shift+B, but there isn't a default for Rebuild. Go to the Tools menu » Options » Environment » Keyboard. Select the "Build.RebuildSolution" command. Add a key binding of Ctrl+Alt+Shift+B.
Clean Solution only deletes the files in the bin
folder. It doesn't delete the files in the obj
folder. When I get weird build errors, I do what I refer to as a "Hard Clean". I delete the bin
and obj
folders from every project folder in my solution.
I used to do this manually, in Windows Explorer. It is a little time consuming, especially if you have a lot of projects. But I have automated it.
I do this with a PowerShell script. And I invoke this from a BAT file. I invoke the BAT file from one of two ways: a custom menu in Visual Studio under Tools, and from Windows Explorer's Send To menu.
First, copy these files. You can copy them anywhere on your computer (e.g. C:\Tools\
).
Second, modify the HardClean.bat file so that it has the correct full path to the ps1 file. Also, you can remove the pause if you want. But I prefer to see the output before the window closes.
Third, add this command to Visual Studio's Tool menu
- Open the "Tools » External Tools…" menu
- Click the Add button
- Set the properties:
- Title:
&Hard Clean
- Command: the full path to the HardClean.bat file
- Arguments:
$(SolutionDir)
- Check Close on exit
Now you can do a hard clean on your solution just by choosing the Tools » Hard Clean menu in Visual Studio.
Fourth, alternatively you can add this to Windows Explorer.
- Open Windows Explorer
- Type "
shell:SendTo
" in the location bar at the top. - Create a shortcut to the HardClean.bat file in this folder. Name the short cut "Hard Clean"
Now you can right click on any solution or project folder and choose "Hard Clean".