| Author: |
Carlos J. Quintero (Microsoft MVP) |
Applies to: |
Microsoft Visual Studio .NET 2002 |
| Date: |
January 2011 |
|
Microsoft Visual Studio .NET 2003 |
| Updated: |
March 2013 |
|
Microsoft Visual Studio 2005 |
| |
|
|
Microsoft Visual Studio 2008 |
| |
|
|
Microsoft Visual Studio 2010 |
| |
|
|
Microsoft Visual Studio 2012 |
Introduction
The automation model (EnvDTE) provides the Solution property in the EnvDTE.DTE
class, which is always instantiated, even when no solution is loaded (it
provides the IsOpen property to know if there is a solution loaded or not). To
create and save a solution, two methods need to be called:
- The Solution.Create method, which receives as parameters the folder where to
create the solution and the name of the solution. However you must notice that:
- This method doesn't actually create the solution on disk.
- The folder of the solution must exist, Visual Studio doesn't create it.
- The Solution.SaveAs method, which receives as parameter the full name (with
path) of the solution file. Notice that you already specified the folder of the
solution and the name of the solution in the previous method, but you need to do
it again in this one. Also, if the folder of the solution doesn't exist, this
method will show a "Save As" prompt dialog.
The following sample shows how to create an empty solution from a Visual Studio
add-in. The code also shows how to get the folder where Visual Studio creates
the solutions by default:
| Language: C# | Copy Code (IE only) |
public class Connect : Object, Extensibility.IDTExtensibility2
{
private _DTE applicationObject;
private AddIn addInInstance;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode,
object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
switch (connectMode)
{
case ext_ConnectMode.ext_cm_AfterStartup:
InitializeAddIn();
break;
case ext_ConnectMode.ext_cm_Startup:
// OnStartupComplete will be called
break;
}
}
public void OnStartupComplete(ref System.Array custom)
{
InitializeAddIn();
}
private void InitializeAddIn()
{
CreateEmptySolution();
}
private void CreateEmptySolution()
{
string solutionFolder;
string solutionName;
string solutionFullFileName;
if (applicationObject.Solution.IsOpen)
{
// Close the solution saving it
applicationObject.Solution.Close(true);
}
// Get the folder where to save the solution
solutionFolder = System.IO.Path.Combine(GetVisualStudioProjectsFolder(), "MySolution");
// Important: if the folder doesn't exist, create it. Otherwise Visual Studio
// doesn't create it and a prompt is shown when the SaveAs method is called
if (!System.IO.Directory.Exists(solutionFolder))
{
System.IO.Directory.CreateDirectory(solutionFolder);
}
solutionName = "MySolution";
solutionFullFileName = System.IO.Path.Combine(solutionFolder, "MySolution.sln");
applicationObject.Solution.Create(solutionFolder, solutionName);
applicationObject.Solution.SaveAs(solutionFullFileName);
}
private string GetVisualStudioProjectsFolder()
{
string version = "";
string result;
Microsoft.Win32.RegistryKey registryKey;
switch (applicationObject.Version)
{
case "7.00":
version = "7.0";
break;
case "7.10":
version = "7.1";
break;
case "8.0":
version = "8.0";
break;
case "9.0":
version = "9.0";
break;
case "10.0":
version = "10.0";
break;
case "11.0":
version = "11.0";
break;
}
registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\VisualStudio\" + version);
result = registryKey.GetValue("VisualStudioProjectsLocation").ToString();
registryKey.Close();
return result;
}
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
public void OnBeginShutdown(ref System.Array custom)
{
}
}
| Language: VB.NET | Copy Code (IE only) |
Public Class Connect
Implements Extensibility.IDTExtensibility2
Dim applicationObject As EnvDTE.DTE
Dim addInInstance As EnvDTE.AddIn
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, _
ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = CType(application, EnvDTE.DTE)
addInInstance = CType(addInInst, EnvDTE.AddIn)
Select Case connectMode
Case ext_ConnectMode.ext_cm_AfterStartup
InitializeAddIn()
Case ext_ConnectMode.ext_cm_Startup
' OnStartupComplete will be called
End Select
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
InitializeAddIn()
End Sub
Private Sub InitializeAddIn()
CreateEmptySolution()
End Sub
Private Sub CreateEmptySolution()
Dim solutionFolder As String
Dim solutionName As String
Dim solutionFullFileName As String
If applicationObject.Solution.IsOpen Then
' Close the solution saving it
applicationObject.Solution.Close(True)
End If
' Get the folder where to save the solution
solutionFolder = System.IO.Path.Combine(GetVisualStudioProjectsFolder(), "MySolution")
' Important: if the folder doesn't exist, create it. Otherwise Visual Studio
' doesn't create it and a prompt is shown when the SaveAs method is called
If Not System.IO.Directory.Exists(solutionFolder) Then
System.IO.Directory.CreateDirectory(solutionFolder)
End If
solutionName = "MySolution"
solutionFullFileName = System.IO.Path.Combine(solutionFolder, "MySolution.sln")
applicationObject.Solution.Create(solutionFolder, solutionName)
applicationObject.Solution.SaveAs(solutionFullFileName)
End Sub
Private Function GetVisualStudioProjectsFolder() As String
Dim version As String = ""
Dim result As String
Dim registryKey As Microsoft.Win32.RegistryKey
Select Case applicationObject.Version
Case "7.00"
version = "7.0"
Case "7.10"
version = "7.1"
Case "8.0"
version = "8.0"
Case "9.0"
version = "9.0"
Case "10.0"
version = "10.0"
Case "11.0"
version = "11.0"
End Select
registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\VisualStudio\" & version)
result = registryKey.GetValue("VisualStudioProjectsLocation").ToString()
registryKey.Close()
Return result
End Function
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
End Class
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|