我已经创建了一个addin应用程序,我正在使用它编程创建一个带有一些文件的解决方案。我必须将它集成到另一个winforms应用程序中。因此,一旦用户单击该按钮,就会动态创建一个带有一些输入的新项目。
在addin项目中,表单包含一个自动调用的方法(OnConnection)。这将在运行时创建项目。该应用程序运行正常。
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
createProjectsFromTemplates(_applicationObject);
}
public void createProjectsFromTemplates(DTE2 dte)
{
try
{
// Create a solution with two projects in it, based on project
// templates.
Solution2 soln = (Solution2)dte.Solution;
string csTemplatePath;
//string vbTemplatePath;
string csPrjPath = "\\\\#####.com\\###\\My Documents\\TestCreateProject";
//string vbPrjPath = "C:\\UserFiles\\user1\\addins\\MyVBProject";
// Get the project template path for a C# console project.
// Console Application is the template name that appears in
// the right pane. "CSharp" is the Language(vstemplate) as seen
// in the registry.
csTemplatePath = soln.GetProjectTemplate("WpfApplication.zip", "CSharp");
System.Windows.Forms.MessageBox.Show("C# template path: " + csTemplatePath);
// Get the project template path for a Visual Basic console
// project.
// "vbproj: is the DefaultProjectExtension as seen in the
// registry.
//vbTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip", "vbproj");
//System.Windows.Forms.MessageBox.Show("Visual Basic template path: " + vbTemplatePath);
// Create a new C# console project using the template obtained
// above.
soln.AddFromTemplate(csTemplatePath, csPrjPath, "NewWCFCSharpAutoGeneratorProject", false);
// Create a new Visual Basic console project using the template
// obtained above.
//soln.AddFromTemplate(vbTemplatePath, vbPrjPath, "New VB Console Project", false);
Project prj;
ProjectItem prjItem;
String itemPath;
// Point to the first project (the Visual Basic project).
prj = soln.Projects.Item(1);
prjItem = prj.ProjectItems.AddFromFileCopy("\\\\####.com\\###\\My Documents\\SampelCSharp.cs");
// Retrieve the path to the class template.
//itemPath = soln.GetProjectItemTemplate("MyClass.zip", "CSharp");
//Create a new project item based on the template, in this
// case, a Class.
//prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass.cs");
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message);
}
}
现在我必须使用来自其他应用程序(Winforms)的add in项目才能在运行时创建项目。我已经构建了addin项目并将.dll文件导入该应用程序。我已经为类创建了一个实例,并获得了“OnConnection”方法。但是我不确定要作为参数传递什么。因为在调试方法时它显示“application”参数在其中携带一些“COM”对象。
此外,如果应用程序运行一次,则在再次执行时创建新项目,它表示文件已存在于路径中。我必须覆盖旧的。
这是什么解决方案?
注意:使用Visual Studio 2012和Framework 3.5
答案 0 :(得分:1)
试试这个:
EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.11.0");
Connect objConnect = new Connect();
Array objArray = null;
objConnect.OnConnection(dte2, ext_ConnectMode.ext_cm_UISetup, null, ref objArray);