简体   繁体   中英

How can I cause visual studio to automatically include a using statement like using System.Diagnostics, in every project?

How can I cause visual studio to automatically include a using statement like using System.Diagnostics, in every project?

I've been developing winforms applications and console applications and would like System.Diagnostics to be imported automatically with a using statement so I don't have to manually put the using statement in.

How can I do that?

You can edit an appropriate class template for the Visual Studio so each new generated class will have using statements that you have put there.

For how to do this, check this question: How do I edit the Visual Studio templates for new C# class/interface?

You need to edit this file:

(YourDriveNameHere):\Program Files (x86)\Microsoft Visual Studio 14.0\
                         Common7\IDE\ItemTemplates\CSharp\Code\1033\Class/Class.cs

To be like this:

using System;
using System.Collections.Generic;
using System.Diagnostics;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

Okay, you can do this the way @ user3185569 told.

Also, there is a file named Class.vstemplate in that same directory. Open it and you can easily edit the following:

<TemplateContent>
        <References>
            <Reference>
                <Assembly>System</Assembly>
            </Reference>
            <Reference>
                <Assembly>System.Diagnostics</Assembly>
            </Reference>
            <Reference>
                <Assembly>System.Web</Assembly>
            </Reference>
            <Reference>
                <Assembly>System.Data</Assembly>
            </Reference>
        </References>

    <ProjectItem ReplaceParameters="true">Class.cs</ProjectItem>
</TemplateContent>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM