天天看點

Visual Studio 2010 Automation and Extensibility (1)

VS 2010的自動化操作。

從啟動visual studio,到建立solution,到建立一個Console Applciation。

代碼中有幾處bug,比如已經存在的目錄需要删除,但是沒有删,沒辦法,手工删除吧。

本來還想添加工程引用,添加引用,利用EditPoint輸入文本。考慮到時間不早了,是以不寫了,以後會繼續寫代碼。 

Module Module1

Sub Main()

' -----------------------------------------------------------------

' (1) Initializes the vs object and launching Visual Studio.

' -----------------------------------------------------------------

Dim vsDTE As EnvDTE.DTE

Dim vsDteType As Type = Type.GetTypeFromProgID("VisualStudio.DTE.10.0")

' Now vs is initialized.

vsDTE = Activator.CreateInstance(vsDteType)

' Set the main window is visible to see the Visual Studio.

vsDTE.MainWindow.Visible = True

' -----------------------------------------------------------------

' (2) Create solution with console template.

' -----------------------------------------------------------------

Dim solnName As String = "MyTestSolution"

Dim solnSavePath As String = "C:/VSProjs/"

Dim consoleAppTemplate As String =

"C:/Program Files/Microsoft Visual Studio 10.0/Common7/IDE/ProjectTemplatesCache/VisualBasic/Windows/1033/ConsoleApplication.zip/consoleapplication.vstemplate"

Dim soln As EnvDTE.Solution = vsDTE.Solution

' Dim soln2 As EnvDTE80.Solution2 = vsDTE.Solution

Dim proj As EnvDTE.Project

Dim proj2 As EnvDTE.Project

'

' Delete the previous folder that created.

'

If System.IO.Directory.Exists("C:/VSProjs") Then

System.IO.Directory.Delete(solnSavePath)

End If

'

' Creates a solution with naming "MyTestSolution" and saved it under c:/VSProjs./

'

soln.Create(solnSavePath, solnName) ' Here, there is a problem, when creating a solution the save as dialog always prompt.

soln.Saved = False

proj = soln.AddFromTemplate(consoleAppTemplate, solnSavePath, "MyTestProj", True)

proj.Save()

proj2 = soln.AddFromTemplate("C:/Program Files/Microsoft Visual Studio 10.0/Common7/IDE/ProjectTemplatesCache/VisualBasic/Windows/1033/ClassLibrary.zip/ClassLibrary.vstemplate", solnSavePath, "MyTestClsLibrary", True)

' Stop to see what will happen.

Stop

End Sub

End Module

繼續閱讀