1

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?

10
  • 2
    possible duplicate of Is there any way to mass organise usings in Visual Studio 2012? Commented Sep 18, 2014 at 13:56
  • 1
    @DGibbs if you actually read the question you would see that they are not the same Commented Sep 18, 2014 at 13:57
  • 1
    Maybe 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 might Commented Sep 18, 2014 at 13:59
  • 1
    @DavidPilkington The part about PowerCommands? ReSharper is also a good suggestion. Commented Sep 18, 2014 at 14:00
  • 1
    There 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;" Commented Sep 18, 2014 at 14:02

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

5 Comments

Looks like it's not possible in C#. See Support for VB.NET's Imported Namespaces feature in C#.
Well that's just horrible.
I saw your update and thought Wow! No more need for 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.
@AndrewMorton there's no way around that one afaik if you want simple, efficient clean up! I never thought twice about which one the question was about, that's an interesting problem and a good clarification to make. You can use using var x = CreatingFunction(); and have it dispose once the function exits; no braces needed in that case.
The 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.