Create blank solution files for VS2010 programmatically? - visual-studio-2010

I am trying to create empty solution file for Visual studio 2010 but I am unable to do so?
We used to create empty solution file for vs2008 by
EnvDTE::_SolutionPtr ptrSoln(_T("VisualStudio.Solution.9.0")); but I am unable to find equivalent for VS2010.
I was able to find how to create project but not Solutions?

I found the answer. Answer lies in previous question mentioned by me - How can I create new blank solution in vs 2008 programmatically?
I forgot to add reference to EnvDTE, EnvDTE80. EnvDTE90 and EnvDTE100 assembly. Also name of solution class is Solution4 instead of Solution3. So code snippet which works is:
string visualStudioProgID = "VisualStudio.Solution.10.0";
Type solutionObjectType = System.Type.GetTypeFromProgID(visualStudioProgID, true);
object obj = System.Activator.CreateInstance(solutionObjectType, true);
Solution4 solutionObject = (Solution4)obj;
solutionObject.Create("C:/", "Test");
solutionObject.SaveAs(#"C:/Test.sln");

Not 100% sure what you're asking. If you just want an empty solution with some pre-created folders, which would be a file just named yourProject.sln, go to whatever primary language you have installed (C++ in my case), and make an empty project. This will give you an empty solution file, with just one folder, it would be named yourProject in my case above, and a few project files related to the language. If you want to add a new project to this solution, go to:
File->Add->New Project. Fill in the rest with whatever application type your project/program needs to be. This is how it's done in C++, I don't know how to do it in, say C#. However, you didn't specify a language... So I'm not sure where to go to help you, Visual Basic? C++?

Related

How to reference TFS item within multiple projects

I have a C# solution I created within Visual Studio 2010 Ultimate that contains multiple projects that make use of the same structures, classes, and enumerated types. I have created a .cs file that contains all of these items, but at the moment I have to make a duplicate of this file for each project. This is obviously less than ideal because I have to remember to update each copy of the file should I make a change or addition.
Is there are way I can have all of the projects reference just one actual copy of this file, so that I only have to update it once and all of the other projects can see this update? A colleague of mine told me it was possible to make some sort of "symbolic" reference in TFS, but since I don't really know what the name of this feature is I don't know how to research it further.
This question has answers that suggest either creating a class library or using "Add as link" when adding an existing item. I suppose I can do this in Visual Studio when not using TFS, but I have posed this as a TFS-specific question. Will this "add as link" option work when TFS is in use or do I have to do something in TFS to get it to honor the link reference?
"Add link" option would definitely work with TFS. You dont have to do anything specific for that. "Add Link" is project specific and it will add a new entry to your project file specifying the location of your .CS file. As long as the file is available in the particular location during server build, there is nothing to worry. Make sure that the Add link makes a relative path to the file and not absolute.
In my company, we have multiple projects within a solution and we share a single "Assemblyversion.cs" for all the projects so that I dont need to write version info in multiple files. We have made use of Add Link and it works great!

Is there a good way to create and use a 'Template' in Visual Studio 2010

Our projects in our company are all built based off a thing we call a 'Project Scaffold'. It's got all the base required code for all sites, basic folder structure and all other things similar to that.
At the moment, we have a repository for this 'Project Scaffold' and each time we want to create a new project, we copy this project into a new folder, and rename all occurrences of the name 'ProjectScaffold' within the project. As you can tell, this is quite time consuming and can sometimes cause errors if we miss out a single occurrence of 'ProjectScaffold'.
This project will need to have all the default dependencies which is why having a full project that we copy is working for us at the moment.
I have looked into the possibility of creating a visual studio template but I can't seem to find a good way of accomplishing it.
We have been thinking if there was a way to possibly implement it through a NuGet Package, however I feel as if this would be either extremely difficult or impossible.
If possible, it would also be something that would be usable in Visual Studio 2011
Simply File-->Export Template :)
You can then customize the generated zip file to suit your needs.
More on this on the Creating Project and Item templates on msdn

Finding the home directory for a Visual Studio 2010 extension

I am making changes to a Visual Studio wizard that creates a project from a template, and needs to add a reference to an assembly to the project that also lives in the extension directory. So I need to set the <hintpath>.
I have not been able to figure out how a running VS extension can discover its extension directory, which is a directory name like this:
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\myCompany\myExtension
Using System.Reflection.Assembly.GetExecutingAssembly().CodeBase yields:
"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\myCompany.myExtension\v4.0_1.0.0.0__936015a19c3638eb\myCompany.myExtension.dll"
Unfortunately, not helpful. Using GetCallingAssembly() is no better--it points at another directory in the MSIL_GAC.
Is there a Visual Studio interface that returns this information? I haven't been able to find it.
If that's not possible, is it at least possible to determine if the extension is running in the experimental instance vs. non-experimental? I could use that information to locate the extension directory.
Maybe look at IInstalledExtension.InstallPath. You can get an IInstalledExtension via an IVsExtensionManager.
Unfortunately, the message in the remarks suggests this is not the right way to do things:
Although this API supports the Extension Manager infrastructure, we recommend that you do not use it because it is subject to change.
EDIT: Here's the code:
static IVsExtensionManager GetExtensionManager()
{
return myPackage.GetService(System.typeof(IVsExtensionManager)) as IVsExtensionManager;
}
static IInstalledExtension GetExtension(string identifier)
{
return GetExtensionManager().GetInstalledExtension(identifier);
}
static string GetExtensionDirectory(string identifier)
{
return GetExtension(identifier).InstallPath;
}
The string identifier is whatever you put in the "ID" field of your extension's source.extension.vsixmanifest file. It defaults to the package GUID.
Sorry for digging up an old answered question...
I was looking into a similar problem, and have implemented the Extension Manager, but decided this is just pretty awful, though if you do want to do use it these links will also help:
https://stackoverflow.com/a/24294185
http://blog.ninlabs.com/2011/04/auto-update-visual-studio-extensions/
However, I decided to look into how the CodeGenerator in Asp.Net.Scaffolding accessed template files. Turns out, very easily...
var path = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "MyPath");
Simples

How do I create a Visual Studio project template that includes linked files?

In Visual Studio 2010, I want to create a project template that includes links to two files that should exist on the system. One of them is a common AssemblyInfo.cs file. Another is the strong name key file (*.snk).
I need these references to be relative, because each developer's workspace will be set up differently. Is it possible for the project template to somehow figure out where these files reside in each developer's environment?
From reading about templates, it sound like they're pretty static so I wonder if tricks can be done to do something like this. If nothing else, I can add bogus references that will cause compilation errors and force the developer to hook these files in. But if I can do it for them, that would be better.
You should set the CreateInPlace property to true in the vstemplate. The documentation says
Specifies whether to create the project and perform parameter replacement in the specified location, or perform parameter replacement in a temporary location and then save the project to the specified location.
If you want relative paths to work, you need the parameter replacement to occur in the place where you're creating the project, not in a temporary location.
Microsoft have confirmed that this is a bug with the extensibility model and project templates. I managed to get round this by using IWizard. I added the following code in the RunFinished method of my IWizard implementation:
//Get linked file directory
string coreDir = Path.GetDirectoryName(MyProject.FullName);
//Data folder
ProjectItem propertiesProjectItem = slSharedProject.ProjectItems.Cast<ProjectItem>().Where(p => p.Name == "Data").First();
propertiesProjectItem.ProjectItems.AddFromFile(coreDir + #"\Service\TheFileIWantToLink.cs");
This code links in a copy of TheFileIWantToLink.cs file to my shared Silverlight project (slSharedProject).
You could try to integrate a Wizard into your Project Template and set the Paths to the linked files. If i remember right you don't have to create an User Inteface; you only have to specify the correct replacements in the replacementsDictionary and VS will replace the values in your Template File. See this or this for further information.

Visual Studio Solution Template - Link Source Control Projects

My team is creating some standard VS solution templates. We have a well-defined project structure, resources, etc. that we need to use every time we start a new project and this is the perfect solution. The basics work nicely.
However, as well as defining folder structure (etc.) it would be nice to be able to import a number of projects from VSS/TFS. We have a number of shared assemblies that will be used by all projects and it would be awesome to add a reference to these projects when creating a new project via our template. Can anyone tell me if this is possible and, if so, how it can be achieved?
I think there are 3 types of items you might want to templatize (is that a word?).
New Solution
New Project added to a solution
New item added to a project
I'm not sure whether its possible to add existing projects to the solution that is created when a project template is run. http://msdn.microsoft.com/en-us/library/ms185308.aspx shows how to create multiple project templates. You may have to either manually add them to the solution or create a script that modifies the .sln file to do that part.
Adding an assembly reference to either a project or item template is easily doable. The project template is pretty simple since you just need to modify your .vstemplate file for the project template(s). See http://msdn.microsoft.com/en-us/library/ms171405.aspx for reference.
Adding a new assembly reference when you add a new item from a template is a bit harder but can also be done. See http://msdn.microsoft.com/en-us/library/ms185290.aspx for more.
Have fun!

Resources