Visual Studio provides numerous ways to add new files such as the New File and Add New Item dialogs. In the post I will provide an overview of those approaches, along with my preferred approach for rapidly adding new files in Visual Studio.
New File dialog
The New File dialog can be accessed by clicking File > New > File… or through the keyboard shortcut Ctrl + N.
The New File dialog presents a list of templates to choose from. After you select a template and click open, the new file is displayed for editing. However, when saving, you must specify the path and file name for the new file. The file is not automatically added to the selected project.
Add New Item dialog
The Add New Item dialog enables you to add an item to the currently selected project. The Add New Item dialog is accessed by first selecting the desired project and then clicking Project > Add New Item… or using the keyboard shortcut Ctrl + Shift + A.
The dialog displays templates relevant to the currently selected project. Simply select the desired template, such as a Class, Interface, or Razor component, then specify the name of the new item, and click Add. The new item will be added to the project and opened for editing.
Add New File Extension
I think it is fair to say that the above two options do not provide a way to rapidly add new files in Visual Studio. Fortunately, the extension Add New File by Mads Kristensen does exactly that. This extension is the fastest and easiest way to add new files to any project.
There are two versions of this extension, Add New File (v3) supporting Visual Studio 2017 and 2019 and Add New File (64-bit) (v4) supporting Visual Studio 2022. This blog post is focused on version 4, although most capabilities are supported in both versions of the extension.
The core features are as follows:
- Easily create any file with any extension, such as
User.razor
- Use glob style syntax to add related files, such as
User.(razor, razor.cs)
- Create files starting with a dot, such as
.gitignore
- Create files without extensions, such as
LICENSE
- Create folders when the entered name ends with a
/
- Create deeper folder structures easily if required, e.g.
/Services/Identity/
- Add multiple files and folders, by separating names with commas, e.g.
Common/, Services/Identity/, README.md
- Create custom templates globally or for a specific project
It is a very powerful extension. In the following sections, I’ll show you everything you need to get started.
Installing
Install the Add New File extension using the Visual Studio Manage Extensions dialog or by manually downloading and running the extension from the Visual Studio Marketplace.
Once installed, the Add New File dialog can be accessed using the keyboard shortcut Shift + F2.
It is very quick and easy to use, just specify the desired file name and hit enter. The extension will create an empty file in the selected folder or in the same folder as the selected file. In the above example, a new file named User.cs will be added to the root folder of the Domain project, and be opened ready for editing.
Creating files
The extension makes it easy to create one or more files. Start by selecting the relevant location within the solution explorer and then pressing Shift + F2 to display the Add New File dialog. For example:
This will result in a UsersController.cs file being added to the Controllers folder.
If necessary, you could also traverse to a parent folder using ../
, for example:
This will result in a GlobalUsings.cs file being added to the parent folder of the Controllers folder (typically the root folder of the project).
You can easily create multiple files by separating items with a comma, for example:
Creating folders
The extension makes it easy to create one or more folders and folder structures as necessary. You can create a new folder by entering the folder name ending with a /
, for example:
In the above example, a new Users folder will be created within the WebUI project.
You can create multiple folders by separating items with a comma, for example:
You can create deeper folder structures by specifying the required path, for example:
Within the WebUI project, both the Models folder and the Common sub-folder will be created.
Creating files and folders
The extension makes it quick and easy to add multiple files, folders, and folder structures in one go. For example:
A new Filters folder will be created (assuming it doesn’t exist) along with a new ExceptionFilter.cs file.
Using the comma separator:
Templates
The Add New File extension supports many templates including C#, JSON, HTML. However, you do not need to choose the template when creating a new file. The extension will choose the appropriate template based on the file name specified.
In this example, the extension will use the C# interface template, since the file name follows the convention for naming interfaces and the file extension is “.cs”. The following file will be created:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Awesome.WebUI.Services;
public interface ICurrentUser
{
}
You can view the built-in templates here; AddAnyFile/src/Templates at master · madskristensen/AddAnyFile (github.com).
The extension also supports adding custom templates to your project. Add new templates and override existing templates by creating a .templates folder within your project and adding template files within that folder. In the following example, I’ll create a custom template for ASP.NET Core API controllers.
Within your WebUI project, create a new .templates folder, and add a new template named controller.txt inside that folder. Using the Add New File extension, you can do this rapidly:
- Select the relevant project within Solution Explorer
- Press Shift + F2
- Type .templates/controller.txt and press Enter
This will create the .templates folder and the new controller.txt template. Build the template as follows:
namespace {namespace};
public class {itemname} : ApiControllerBase
{
$
}
This template will be used when creating controllers. For example, a new UsersController.cs. The above template will create a new controller that derives from ApiControllerBase
(a custom controller base class for my project). The {namespace} tokens will be replaced with the relevant namespace and the {itemname} tokens will be replaced with the file name without the file extension.
Once the template has been saved it is ready to use You don’t need to restart Visual Studio. Create a new controller as follows:
namespace AwesomeProject.WebUI.Controllers;
public class UsersController : ApiControllerBase
{
}
In the above example, the template controller.txt was selected as the file name ended with Controller. You can also create templates to match on extension, for example, md.txt to match the Markdown extension “.md”. Finally, you can also create templates for an exact match, for example, the dockerfile.txt template could be used when creating a Dockerfile.
Next steps
In this post, I have demonstrated how to rapidly add new files and folders with Visual Studio using the Add New File Extension by Mads Kristensen. If you would like to learn more, take a look at some of the following resources:
- Add New File – Visual Studio Marketplace (Visual Studio 2017 and 2019)
- Add New File (64-bit) – Visual Studio Marketplace (Visual Studio 2020)
- madskristensen/AddAnyFile: A Visual Studio extension (github.com)
I’d love to hear from you too. What do you think? Will this improve your productivity? Do you have a better approach?
Thanks for reading, please feel free to post any questions or comments below.
Yeah. This is the thing that makes my day to day work much easier.