您的位置:首页 > 其它

Creating a New Project

2008-12-18 17:11 295 查看

CreatingaNewProject

Toprogrammaticallycreateaproject

StartVisualStudioandcreateanewVisualStudioadd-inproject.

Addthecodebelowtotheadd-in'sConnectclass.

Runtheadd-inprojectandactivateitintheAdd-InManager.

Todothis,clickAdd-InManagerontheToolsmenuandthenchecktheboxnexttotheadd-intoactivateit.

Example

ThefollowingexampleusesGetProjectTemplateandAddFromTemplatetocreatetwonewconsoleprojects,oneVisualBasicandtheotherVisualC#,inasolution.

VisualBasic

PublicSubOnConnection(ByValapplicationAsObject,ByVal_
connectModeAsext_ConnectMode,ByValaddInInstAsObject,_
ByRefcustomAsArray)ImplementsIDTExtensibility2.OnConnection
_applicationObject=CType(application,DTE2)
_addInInstance=CType(addInInst,AddIn)
createProjectsFromTemplates(_applicationObject)
EndSub

SubcreateProjectsFromTemplates(ByValdteAsDTE2)
Try
'Createasolutionwithtwoprojectsinit,basedonproject
'templates.
DimsolnAsSolution2=CType(DTE.Solution,_
Solution2)
DimcsTemplatePathAsString
DimvbTemplatePathAsString
DimcsPrjPathAsString=_
"C:/UserFiles/kempb/addins/MyCSProject"
DimvbPrjPathAsString=_
"C:/UserFiles/kempb/addins/MyVBProject"

'GettheprojecttemplatepathforaC#consoleproject.
'ConsoleApplicationisthetemplatenamethatappearsinthe
'rightpane,"CSharp"istheLanguage(vstemplate)asseenin
'theregistry.
csTemplatePath=soln.GetProjectTemplate_
("ConsoleApplication.zip","CSharp")
MsgBox("C#templatepath:"&csTemplatePath)
'GettheprojecttemplatepathforaVisualBasic
'consoleproject.
'"vbproj:istheDefaultProjectExtensionasseeninthe
'registry.
vbTemplatePath=soln.GetProjectTemplate_
("ConsoleApplication.zip","vbproj")
MsgBox("VisualBasictemplatepath:"&vbTemplatePath)
'CreateanewC#consoleprojectusingthetemplateobtained
'above.
soln.AddFromTemplate(csTemplatePath,csPrjPath,_
"NewCSharpConsoleProject",False)
'CreateanewVisualBasicconsoleprojectusingthetemplate
'obtainedabove.
soln.AddFromTemplate(vbTemplatePath,vbPrjPath,_
"NewVisualBasicConsoleProject",False)
CatchexAsSystem.Exception
MsgBox("ERROR:"&ex.ToString)
EndTry

EndSub



C#


publicvoidOnConnection(objectapplication,ext_ConnectMode
connectMode,objectaddInInst,refArraycustom)
{
_applicationObject=(DTE2)application;
_addInInstance=(AddIn)addInInst;
createProjectsFromTemplates(_applicationObject);
}

publicvoidcreateProjectsFromTemplates(DTE2dte)
{
try
{
//Createasolutionwithtwoprojectsinit,basedonproject
//templates.
Solution2soln=(Solution2)dte.Solution;
stringcsTemplatePath;
stringvbTemplatePath;
stringcsPrjPath="C://UserFiles//kempb//addins//MyCSProject";
stringvbPrjPath="C://UserFiles//kempb//addins//MyVBProject";
//GettheprojecttemplatepathforaC#consoleproject.
//ConsoleApplicationisthetemplatenamethatappearsin
//therightpane."CSharp"istheLanguage(vstemplate)asseen
//intheregistry.
csTemplatePath=soln.GetProjectTemplate("ConsoleApplication.zip",
"CSharp");
System.Windows.Forms.MessageBox.Show("C#templatepath:"+
csTemplatePath);
//GettheprojecttemplatepathforaVisualBasicconsole
//project.
//"vbproj:istheDefaultProjectExtensionasseeninthe
//registry.
vbTemplatePath=soln.GetProjectTemplate("ConsoleApplication.zip",
"vbproj");
System.Windows.Forms.MessageBox.Show("VisualBasictemplatepath:"+
vbTemplatePath);
//CreateanewC#consoleprojectusingthetemplateobtained
//above.
soln.AddFromTemplate(csTemplatePath,csPrjPath,"NewCSharp
ConsoleProject",false);
//CreateanewVisualBasicconsoleprojectusingthetemplate
//obtainedabove.
soln.AddFromTemplate(vbTemplatePath,vbPrjPath,"NewVBConsole
Project",false);
}
catch(System.Exceptionex)
{
System.Windows.Forms.MessageBox.Show("ERROR:"+ex.Message);
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: