Introduction
There are plenty of tips and tricks for Visual Studio while the tips presented here are not commonly known.
Adding a project reference
The conventional method is to, with Solution Explorer open, right click on the project, Add, project reference and check the project to add.
Alternate, with Solution Explorer open, single click the project to add, drag to the project to add too and release the mouse, done.
Namespace declarations
By default, block namespaces are used in Visual Studio 2022. If you rather the default can be changed to File scoped
via Visual Studio Options.
- From Visual Studio menu, select Tools
- Options
- Text Editor
- C#
- Code Style
- General
External Tools
You can call external tools from inside Visual Studio by using the Tools menu. A few default tools are available from the Tools menu, and you can customize the menu by adding other executables of your own.
Example 1
Creating a menu item that when clicked will open the application folder.
Property | Value |
---|---|
Title | Open Bin folder |
Command | C:\Windows\explorer.exe |
Arguments | $(BinDir) |
Initial directory | $(BinDir) |
Example 2
Calling a helper utility, in this case I have one for generating an appsettings.json with a connection string
Property | Value |
---|---|
Title | Generate appsettings without encryption |
Command | C:\OED\DotnetLand\VS2022\AppsettingsSqlServerSolution\GenerateAppSettingsSqlServer\bin\Debug\net6.0\GenerateAppSettingsSqlServer.exe |
Arguments | -f $(ProjectDir) -u no -l yes |
Initial directory | $(ProjectDir) |
Close on exits | ✔️ |
Full source for this utility which currently does not do web, I created this for code samples for console projects but would be easy enough to work into web apps.
Visual Studio Toolbox
You can add code snippets to the toolbox, find some code that you use frequently, highlight the code and drag to the toolbox. Optionally, right click on the newly added item and give it a meaningful name.
Example, you have several project written in .NET Core 5 and want to move to .NET Core 7. Create a new .NET Core 7 project, double click on the project name in Solution Explorer.
Highlight the following and drag to the toolbox
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Open the old .NET Core 5 project via double clicking on the project name in Solution Explorer and comment out the old setting.
<!--<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>-->
Next, drag the new settings into the project file, save, rebuild. Sometimes Visual Studio will indicate a problem which can be resolved by running.
dotnet restore
This is another place an external tool can help.
Property | Value |
---|---|
Title | Restore current project |
Command | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe |
Arguments | dotnet restore |
Initial directory | $(ProjectDir) |
Visual Studio Performance Manager
Have you noticed Visual Studio taking longer to startup? Many times this comes from a Visual Studio extension taking time to load or a window like Test Explorer loading.
To diagnose, open Performance Manager under the help menu. Look for items as shown below and make a decision on startup behavior.
If this does not resolve the issue, disable one extension at a time, reload Visual Studio to see if the extension was the problem. If it was consider contacting the author indicating what you learned.
Multi-carets editing
There may be time you want to edit multiple places that are unaligned, you can use the multi-cursor edit. Press CTRL + ALT and click where you want to add a caret. Then make edits or change formatting as shown below. After setting positons, TAB is pressed.
Include class link in a a markdown file
With a markdown file open, select a class in Solution Explorer and drag to a location in the markdown file, release. Depending on the file name you may need to remove spaces as per the video below.
Tips and Tricks - Visual Studio Blog
Check out the various tips and tricks from Microsoft here.
Developer news
Five shortcuts to boost your productivity
Videos
- Microsoft Visual Studio Tips & Tricks videos