I'm using VisualStudio 2013, and I've refactored some C# code that requires adding a using statement on almost all of my files. Is there any way of doing that without editing each one?
-
2possible duplicate of Is there any way to mass organise usings in Visual Studio 2012?DGibbs– DGibbs2014-09-18 13:56:55 +00:00Commented Sep 18, 2014 at 13:56
-
1@DGibbs if you actually read the question you would see that they are not the sameDavid Pilkington– David Pilkington2014-09-18 13:57:38 +00:00Commented Sep 18, 2014 at 13:57
-
1Maybe reSharper is a solution: jetbrains.com/resharper It works well with using in single files, it can find unused usings. Have not tried if adding works, but it mightPieterSchool– PieterSchool2014-09-18 13:59:50 +00:00Commented Sep 18, 2014 at 13:59
-
1@DavidPilkington The part about PowerCommands? ReSharper is also a good suggestion.DGibbs– DGibbs2014-09-18 14:00:21 +00:00Commented Sep 18, 2014 at 14:00
-
1There might be a better way, but you could do a find and replace in project with regular expressions. Replace "using System;" with "using System;\r\nusing YourNameSpace;"Travis Wolfe– Travis Wolfe2014-09-18 14:02:42 +00:00Commented Sep 18, 2014 at 14:02
1 Answer
Right-click on your project in Solution Explorer. Click on the 'References' tab. Find your namespace in the 'Imported Namespaces' listbox and check the box next to it. This will now import the namespace for all of the project.
NOTE: This works in VB, not sure if it works in C#. Please verify and let me know.
Update 2024:
Someone liked this recently, so I thought I'd update it.
C# 10+ has a concept of global usings.
This will import the System namespace for all files in the project:
global using System;
You can also use implicit usings in the project. There's a chart that says what namespaces these are, as it's based on the project type.
In the project file:
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
You can also add usings in the project file that aren't included with the defaults:
<ItemGroup>
<Using Include="My.Custom.Namespace" />
</ItemGroup>
Sources:
5 Comments
using (resource) {...}, it'll do it itself! And then I realised that the using directive is a different thing from a using statement. Let's see if the OP replies to my comment on their question.using var x = CreatingFunction(); and have it dispose once the function exits; no braces needed in that case.global usingdirectives can be in ANY file in the project. Simply create a file called 'GlobalUsings.cs' (or what you like) and add it to the project, then add all your global using there.