自动生成解决方案和项目并生成项目
2006-11-01 14:00
197 查看
using System.IO;
using System.Text;
using System.Windows.Forms;
using VSLangProj;
namespace CodeToolAddin
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
Read me for Add-in installation and setup information.#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup project
// by right clicking the project in the Solution Explorer, then choosing install.
#endregion
/**//// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("C09025F7-8F9D-4BE3-BB26-413D98B0D933"), ProgId("CodeToolAddin.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2, IDTCommandTarget
{
/**//// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/**//// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[]
{ };
Commands commands = applicationObject.Commands;
_CommandBars commandBars = applicationObject.CommandBars;
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, the Add-in or its commands may become unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) You add new commands or modify commands already defined.
// You will need to re-register the Add-in by building the CodeToolAddinSetup project,
// right-clicking the project in the Solution Explorer, and then choosing install.
// Alternatively, you could execute the ReCreateCommands.reg file the Add-in Wizard generated in
// the project directory, or run 'devenv /setup' from a command prompt.
try
{
Command command = commands.AddNamedCommand(addInInstance, "CodeToolAddin", "CodeToolAddin", "Executes the command for CodeToolAddin", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
CommandBar commandBar = (CommandBar)commandBars["Tools"];
CommandBarControl commandBarControl = command.AddControl(commandBar, 1);
}
catch(System.Exception /**//*e*/)
{
}
}
}
/**//// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}
/**//// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}
/**//// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface.
/// Receives notification that the host application has completed loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
}
/**//// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
}
/**//// <summary>
/// Implements the QueryStatus method of the IDTCommandTarget interface.
/// This is called when the command's availability is updated
/// </summary>
/// <param term='commandName'>
/// The name of the command to determine state for.
/// </param>
/// <param term='neededText'>
/// Text that is needed for the command.
/// </param>
/// <param term='status'>
/// The state of the command in the user interface.
/// </param>
/// <param term='commandText'>
/// Text requested by the neededText parameter.
/// </param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
{
if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "CodeToolAddin.Connect.CodeToolAddin")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
}
}
}
/**//// <summary>
/// Implements the Exec method of the IDTCommandTarget interface.
/// This is called when the command is invoked.
/// </summary>
/// <param term='commandName'>
/// The name of the command to execute.
/// </param>
/// <param term='executeOption'>
/// Describes how the command should be run.
/// </param>
/// <param term='varIn'>
/// Parameters passed from the caller to the command handler.
/// </param>
/// <param term='varOut'>
/// Parameters passed from the command handler to the caller.
/// </param>
/// <param term='handled'>
/// Informs the caller if the command was handled or not.
/// </param>
/// <seealso class='Exec' />
public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "CodeToolAddin.Connect.CodeToolAddin")
{
handled = true;
MessageBox.Show(applicationObject.FullName);
CreateProject();
return;
}
}
}
public string RenderTemplate(string templateFile, string name, string destdir)
{
FileStream fs = new FileStream(templateFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);
string strFile = sr.ReadToEnd();
sr.Close();
fs.Close();
strFile = strFile.Replace("[!output SAFE_NAMESPACE_NAME]", name);
strFile = strFile.Replace("[!output SAFE_CLASS_NAME]", name + "Class");
FileStream fs2 = new FileStream(destdir + name + ".cs", FileMode.CreateNew, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs2,Encoding.Default);
sw.Write(strFile);
sw.Close();
fs2.Close();
return (destdir + name + ".cs");
}
private void CreateProject()
{
string name = "TestDTEPro";
Solution sol = applicationObject.Solution;
sol.Create("f://temp","TestDTE");
string path = @"C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/VC#Wizards/DefaultDll.csproj";
Project theProject = sol.AddFromTemplate(path, "f://temp//TestDTE", name, true);
VSProject myVSProject = (VSProject)theProject.Object;
string templateFile = @"C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/VC#Wizards/CSharpDLLWiz/Templates/2052/file1.cs";
string destination = @"f:/temp/TestDTE/";
string filename = RenderTemplate(templateFile, "TestClass", destination);
ProjectItem theItem = myVSProject.Project.ProjectItems.AddFromFile(filename);
myVSProject.Project.Save(destination + name + ".csproj");
myVSProject.Refresh();
sol.SaveAs(@"f:/temp/newsolution.sln");
//MessageBox.Show(theProject.FullName);
EnvDTE.Window solutionExplorer = applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
solutionExplorer.Visible = true;
solutionExplorer.Activate();
SolutionBuild sb = sol.SolutionBuild;
sb.BuildProject(sb.ActiveConfiguration.Name,theProject.UniqueName,false);
}
private _DTE applicationObject;
private AddIn addInInstance;
}
}
using System.Text;
using System.Windows.Forms;
using VSLangProj;
namespace CodeToolAddin
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
Read me for Add-in installation and setup information.#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup project
// by right clicking the project in the Solution Explorer, then choosing install.
#endregion
/**//// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("C09025F7-8F9D-4BE3-BB26-413D98B0D933"), ProgId("CodeToolAddin.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2, IDTCommandTarget
{
/**//// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/**//// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[]
{ };
Commands commands = applicationObject.Commands;
_CommandBars commandBars = applicationObject.CommandBars;
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, the Add-in or its commands may become unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) You add new commands or modify commands already defined.
// You will need to re-register the Add-in by building the CodeToolAddinSetup project,
// right-clicking the project in the Solution Explorer, and then choosing install.
// Alternatively, you could execute the ReCreateCommands.reg file the Add-in Wizard generated in
// the project directory, or run 'devenv /setup' from a command prompt.
try
{
Command command = commands.AddNamedCommand(addInInstance, "CodeToolAddin", "CodeToolAddin", "Executes the command for CodeToolAddin", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
CommandBar commandBar = (CommandBar)commandBars["Tools"];
CommandBarControl commandBarControl = command.AddControl(commandBar, 1);
}
catch(System.Exception /**//*e*/)
{
}
}
}
/**//// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}
/**//// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}
/**//// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface.
/// Receives notification that the host application has completed loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
}
/**//// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
}
/**//// <summary>
/// Implements the QueryStatus method of the IDTCommandTarget interface.
/// This is called when the command's availability is updated
/// </summary>
/// <param term='commandName'>
/// The name of the command to determine state for.
/// </param>
/// <param term='neededText'>
/// Text that is needed for the command.
/// </param>
/// <param term='status'>
/// The state of the command in the user interface.
/// </param>
/// <param term='commandText'>
/// Text requested by the neededText parameter.
/// </param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
{
if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "CodeToolAddin.Connect.CodeToolAddin")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
}
}
}
/**//// <summary>
/// Implements the Exec method of the IDTCommandTarget interface.
/// This is called when the command is invoked.
/// </summary>
/// <param term='commandName'>
/// The name of the command to execute.
/// </param>
/// <param term='executeOption'>
/// Describes how the command should be run.
/// </param>
/// <param term='varIn'>
/// Parameters passed from the caller to the command handler.
/// </param>
/// <param term='varOut'>
/// Parameters passed from the command handler to the caller.
/// </param>
/// <param term='handled'>
/// Informs the caller if the command was handled or not.
/// </param>
/// <seealso class='Exec' />
public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "CodeToolAddin.Connect.CodeToolAddin")
{
handled = true;
MessageBox.Show(applicationObject.FullName);
CreateProject();
return;
}
}
}
public string RenderTemplate(string templateFile, string name, string destdir)
{
FileStream fs = new FileStream(templateFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);
string strFile = sr.ReadToEnd();
sr.Close();
fs.Close();
strFile = strFile.Replace("[!output SAFE_NAMESPACE_NAME]", name);
strFile = strFile.Replace("[!output SAFE_CLASS_NAME]", name + "Class");
FileStream fs2 = new FileStream(destdir + name + ".cs", FileMode.CreateNew, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs2,Encoding.Default);
sw.Write(strFile);
sw.Close();
fs2.Close();
return (destdir + name + ".cs");
}
private void CreateProject()
{
string name = "TestDTEPro";
Solution sol = applicationObject.Solution;
sol.Create("f://temp","TestDTE");
string path = @"C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/VC#Wizards/DefaultDll.csproj";
Project theProject = sol.AddFromTemplate(path, "f://temp//TestDTE", name, true);
VSProject myVSProject = (VSProject)theProject.Object;
string templateFile = @"C:/Program Files/Microsoft Visual Studio .NET 2003/VC#/VC#Wizards/CSharpDLLWiz/Templates/2052/file1.cs";
string destination = @"f:/temp/TestDTE/";
string filename = RenderTemplate(templateFile, "TestClass", destination);
ProjectItem theItem = myVSProject.Project.ProjectItems.AddFromFile(filename);
myVSProject.Project.Save(destination + name + ".csproj");
myVSProject.Refresh();
sol.SaveAs(@"f:/temp/newsolution.sln");
//MessageBox.Show(theProject.FullName);
EnvDTE.Window solutionExplorer = applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
solutionExplorer.Visible = true;
solutionExplorer.Activate();
SolutionBuild sb = sol.SolutionBuild;
sb.BuildProject(sb.ActiveConfiguration.Name,theProject.UniqueName,false);
}
private _DTE applicationObject;
private AddIn addInInstance;
}
}
相关文章推荐
- [Java] 自动生成visual studio项目文件
- Eclipse导入开源项目报错,R文件也无法自动生成
- JFinal后台权限项目启动时自动生成
- vs2008 c#项目调试dll源码,问题:“若要调试此模块,请将其项目生成配置更改为“调试”模式” 的解决方案
- 自动编译VS解决方案时生成32位和64位版本
- android新建项目时gen目录下的R文件没有自动的生成
- 用mybatis的generator插件在项目中自动生成dao及entity
- 一个自动生成测试用例的项目(github一个大神,曾经阿里P6的职位都没去的大神)
- maven项目使用mybatis-generator自动生成代码
- 在打开vs解决方案时,怎样让所以打开的项目自动折叠
- 没有为此解决方案配置选中要生成的项目
- 自动生成.NET项目帮助生成
- ADT14或者15,修改代码后bin目录下不自动生成apk的解决方案
- VS解决方案添加的项目在生成dll时设置
- 解决android 项目在eclipse中无法自动生成R类方法
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- Android中R.java没有自动生成解决方案
- DotNet 项目开发文档的自动生成和相关工具的使用
- Intelij IDEA 导入 Maven 项目后自动切换 JDK 到 1.5 的解决方案
- entity framework自动生成数据库外键“表名_外键”的解决方案