简体   繁体   English

如何让visual studio在每个项目中自动包含使用System.Diagnostics之类的using语句?

[英]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? 如何让visual studio在每个项目中自动包含使用System.Diagnostics之类的using语句?

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. 我一直在开发winforms应用程序和控制台应用程序,并希望使用using语句自动导入System.Diagnostics,因此我不必手动输入using语句。

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. 您可以为Visual Studio编辑适当的类模板,以便每个新生成的类都具有您放在那里的using语句。

For how to do this, check this question: How do I edit the Visual Studio templates for new C# class/interface? 有关如何执行此操作,请检查以下问题: 如何编辑新C#类/接口的Visual Studio模板?

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. 好的,你可以像@ user3185569告诉的那样做。

Also, there is a file named Class.vstemplate in that same directory. 此外,在同一目录中有一个名为Class.vstemplate的文件。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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