Visual Studio tips 1
Tags: programming, productivity, csharp
Published: 2023-01-23 19:05:30

## 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](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager?view=vs-2022) 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.

![Drag and drop gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucd72huiu49ah2we6kpq.gif) 

## 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.

1. From Visual Studio menu, select Tools
1. Options
1. Text Editor
1. C#
1. Code Style
1. General


![screenshot for file scoped](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5krb1nv4u71c2as2btd.png)

## 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 | :heavy_check_mark: |

[Full source](https://github.com/karenpayneoregon/appsettings-external-tool) 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.

![utility](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u574dwc02k63h1wozfyg.png)

## 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

```xml
<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.

```xml
<!--<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.

```powershell
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](https://learn.microsoft.com/en-us/visualstudio/ide/optimize-visual-studio-startup-time?view=vs-2022) under the help menu. Look for items as shown below and make a decision on startup behavior.


![Performance Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhjwb8ij7bwp5g2qp7n3.png)

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 <kbd>CTRL</kbd> + <kbd>ALT</kbd> and click where you want to add a caret. Then make edits or change formatting as shown below. After setting positons, <kbd>TAB</kbd> is pressed.


![Mutli-carte example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7od3c2y3fos8djbx0uko.gif)

## 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.


![Video](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70i57fwp0ae5m7kcin6h.gif)


## Tips and Tricks - Visual Studio Blog

Check out the various tips and tricks from Microsoft [here](https://devblogs.microsoft.com/visualstudio/tag/tips-and-tricks/).

## Developer news

[Five shortcuts to boost your productivity](https://devblogs.microsoft.com/visualstudio/the-visual-studio-editor-can-do-that/?cid=vs_developer_news)

## Videos

- Microsoft [Visual Studio Tips & Tricks](https://www.youtube.com/playlist?list=PLReL099Y5nRc-zbaFbf0aNcIamBQujOxP) videos