I recently noticed that Visual Studio 2010 (Professional) inserts a FileHeader automatically in new class files. I don't know when this started but some time ago this wasn't enabled. Also, since then the using directives are added after the namespace.
This is the way the file looks like after generation:
// -----------------------------------------------------------------------
// <copyright file="Class1.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace MyNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class Class1
{
}
}
And thats the way it should look like (and how I want it):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyNamespace
{
public class Class1
{
}
}
I disabled all extensions and all plugins, but the problem stays. Hopefully anyone here can tell my how to restore the previoous behaviour.
Thanks in advance
Visual studio uses templates to generate new files. You can read about how to create your own templates here http://stevesmithblog.com/blog/how-to-fix-visual-studio-file-templates/. The default templates are commonly stored in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates and it is these you'll want to edit if you want to globally replace the format.
For me the file headers showed up after installing Resharper and Stylecop.
To get rid of them I disabled the header inclusions in Resharper (Code Cleanup section) and also made sure the file header rules are no longer enforced in StyleCop. Next I modified the file templates stimms mentioned earlier. And finally I had to run devenv.exe /installvstemplates from the command line for Visual Studio to register the changes.
I think that is a ReSharper thing, Check your ReSharper settings.
http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Usage_Scenarios__Configuring_and_Inserting_a_Default_File_Header.html
http://blogs.jetbrains.com/dotnet/2010/12/automating-file-header-information-with-resharper/
Related
I want to save space while printing source code in visual studio. Is it possible to combine multiple using namespace statements at the beginning of *.cs file to single line using resharper in visualstudio, or using some script that can be run using Resharper code cleanup. I want
using System;
using System.Collections.Generic;
using System.Net;
to be formatted as
using System;using System.Collections.Generic;using System.Net;
I've created a new template by "File -> Export template" command. I want source file name to be such as project name, so, I've edited ".vstemplate" file. But now - how do I use this updated template? Seems like VS keeps copies of templates somewhere else, because after restarting VS - nothing changed.
By the way, in VS2012 by developer command prompt was possible to install modified templates by the command "devenv /installvstemplates". Now, this command is gone, it is not a command or a file.
So, how can I use this updated template file?
The Community/Express (free) versions do not support the /installvstemplates options.
If you are using another version, the steps for customising an existing template are as follows (taken from my blog here: http://blog.hitechmagic.com/?p=527):
1. Find the files
The files have not moved around much so are currently in C:\<Program Files>\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
(in VS2012 they are in C:\<Program Files>\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033)
The file you want is in an appropriately named folder (no more ZIP files!). If you open the Class folder you will find the following 2 files:
Class.cs
Class.vstemplate
2. Backup the original files!
Very important step as things do go wrong. Backup the original files e.g. by copying it to Class.cs. Orig so that it is not recognised by Visual Studio.
3. Change the Class.cs template file
This is where you get to be creative. You will notice the original looks something like this:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}
You might want something more like this:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
public class $safeitemrootname$
{
#region Constants
#endregion Constants
#region Fields
#endregion Fields
#region Properties
#endregion Properties
#region Constructors
#endregion Constructors
#region Public methods
#endregion Public methods
#region Class override methods
#endregion Class override methods
#region Class extensions - virtual methods
#endregion Class extensions - virtual methods
#region Private helper methods
#endregion Private helper methods
}
}
You will notice a number of Macros $ commands in the template. The complete list is here: http://msdn.microsoft.com/en-us/library/eehb4faa.aspx.
4. Save your changes
Simply save your edited files (again, there is no longer a ZIP file to re-compress with VS 2012/2013).
5. Tell Visual Studio about the changes
Your new changes will not be loaded unless you explicitly tell Visual Studio to reload all templates.
Close Visual Studio (or the change swill not show until next time you run it)
Open a command prompt (you should run this as Administrator if you are not an admin of the machine).
Change to the IDE folder a few levels above the template folder (e.g. to C:\<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE)
Run the following command: devenv.exe /installvstemplates
Viola! There you have your own custom template for new classes.
6. Automate repetitive/fiddly tasks
If you are going to do this a lot I would suggest creating a batch file, e.g. in your template folder, to run the update step for you. For example create a text file called UpdateTemplates.bat containing the following 3 lines:
cd "C:\Prograsm Files (x86)\Microsoft Visual Studio 12.0\Common\IDE\"
devenv.exe /installvstemplates
pause
When we add a new class in visual studio, we get a template code
namespace MyProject
{
class Class1
{
}
}
How visual studio generates this code? Does it use CodeDOM or T4Template or something else?
Visual Studio has its own project and file templates. The C# class file template, for example, is included in a .zip file with a xml metadata file and the class template:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}
Visual Studio most likely does a simple string replace of the various parameters defined between the $ dollars when it creates a new file based on these templates.
If you look inside the Visual Studio folder you can find various .zip files that contain the file and project templates. The file templates for Visual Studio 2010 are contained inside subfolders inside the parent folder:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates
Hi I've created a new WP7 project. Then I add a new project of type Windows Phone Class Library and by default the project has System.Net, System.Windows and System.Xml references beside other references. I don't need them in my Class Library so I delete them but when I create a new class Visual Studio create the following class :
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace WP7
{
public class Class1
{
}
}
but I've already removed the references System.Net & System.Windows. I'm getting an error when I try to compile the solution.
How I can modify the default template ?
I want to be something like
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
namespace WP7
{
public class Class1
{
}
}
I think this is a bug in VS2010. It doesn't respect the the current references of the project when adding a new class. How can I report this bug to MS ?
this is just how item templates and project templates work. You could create your own set, but that just isn't going to be quick and easy.
The idea is that these assemblies are needed in the majority of apps.
Since they are present on the device and not packaged into your .XAP, there is no net effect to your app.
I am using F# and i found that the available templates were a bit to sparse for my liking, and i want to make more. How would i go about doing that? also, how would i install these templates after I've made them?
In Visual Studio's path you will find the default templates, these are a set of zip files that get expanded into the template cach.
They are stored in
Item Templates - %VSInstallDir%\Common7\IDE\ItemTemplates\
Project Templates - %VSInstallDir%\Common7\IDE\ProjectTemplates\
Extracting the {{.zip}} in question and recompressing with the modified contents will update the template. You can also copy these files to one of the respective template folders in %USERPROFILE%\Documents\Visual Studio 2010.
For information on building templates have a look at Visual Studio Templates on MSDN.
You then need to tell VS to rebuild the cache.
Open a visual studio command line shell
Execute devenv /installvstemplates
You can also use the "Export Template..." wizard from the file menu, however the exported template loses original content such as if statements.
I ran into problems with this and multiple custom templates. Each template (e.g. vstemplate + cs file) should be in it's own zip file. If you put several into the same zip it won't pick up any of them.
I also found that if you put them in:
$My Documents$\Visual Studio 2010\Templates\ItemTemplates
then you wont need run the command (devenv /installvstemplates) mentioned by Brett. Presumably this is only when modifying the existing ones in the install folder.
Here's a sample that I use for knocking up NUnit tests:
Code file (with .cs/relevant extension):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace $rootnamespace$
{
[TestFixture, Category("issue")]
public class $safeitemname$
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test()
{
}
}
}
Template file (with .vstemplate extension):
<VSTemplate Version="3.0.0" Type="Item"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" >
<TemplateData>
<DefaultName>ATest.cs</DefaultName>
<Name>NUnit test</Name>
<Description>
with [TestFixture] attribute set on class and one empty method
called Test that has [Test] attribute
</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>someIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<References />
<ProjectItem SubType="Code" TargetFileName="$fileinputname$.cs"
ReplaceParameters="true">TheCodeFile.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
I'd try http://msdn.microsoft.com/en-us/library/ms185291.aspx - it seems like a good starting point, at least.
I was missing SharePoint 2010 and MOSS 2007 templates in Visual Studio 2010 development tool after my SharePoint installation. I reinstalled VS and found they were available. It looks like during installation of SharePoint pre-requisites for some reason I missed those templates from getting installed.
You can get the complete example implemented here: Multi-Project Templates with Wizard: Visual Studio 2010 Sample
You can download and install VS 2010 SDK (or VS 2010 SP1 SDK) which contains new project templates such as Item Template template and Project Template template (I don't know if there's version for earlier versions of VS and if these templates are only for C# or not).
Visual Studio 2010 SP1 SDK:
http://www.microsoft.com/download/en/details.aspx?id=21835
Visual Studio 2010 SDK:
http://www.microsoft.com/download/en/details.aspx?id=2680