自己做的一个例子(自动生成解决方案和项目并生成项目)

本文介绍了一个使用Visual Studio Add-in创建项目的示例代码,展示了如何通过Add-in实现项目及文件模板的自动生成,并提供了详细的步骤说明。
None.gifusing System.IO;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using VSLangProj;
None.gif
None.gif
namespace CodeToolAddin
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
using System;
InBlock.gif    
using Microsoft.Office.Core;
InBlock.gif    
using Extensibility;
InBlock.gif    
using System.Runtime.InteropServices;
InBlock.gif    
using EnvDTE;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Read me for Add-in installation and setup information.#region Read me for Add-in installation and setup information.
InBlock.gif    
// When run, the Add-in wizard prepared the registry for the Add-in.
InBlock.gif    
// At a later time, if the Add-in becomes unavailable for reasons such as:
InBlock.gif    
//   1) You moved this project to a computer other than which is was originally created on.
InBlock.gif    
//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
InBlock.gif    
//   3) Registry corruption.
InBlock.gif    
// you will need to re-register the Add-in by building the MyAddin21Setup project 
InBlock.gif    
// by right clicking the project in the Solution Explorer, then choosing install.
ExpandedSubBlockEnd.gif
    #endregion

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///   The object for implementing an Add-in.
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <seealso class='IDTExtensibility2' />

InBlock.gif    [GuidAttribute("C09025F7-8F9D-4BE3-BB26-413D98B0D933"), ProgId("CodeToolAddin.Connect")]
InBlock.gif    
public class Connect : Object, Extensibility.IDTExtensibility2, IDTCommandTarget
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        Implements the constructor for the Add-in object.
InBlock.gif        
///        Place your initialization code within this method.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Connect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///      Implements the OnConnection method of the IDTExtensibility2 interface.
InBlock.gif        
///      Receives notification that the Add-in is being loaded.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='application'>
InBlock.gif        
///      Root object of the host application.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='connectMode'>
InBlock.gif        
///      Describes how the Add-in is being loaded.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='addInInst'>
InBlock.gif        
///      Object representing this Add-in.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='IDTExtensibility2' />

InBlock.gif        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            applicationObject 
= (_DTE)application;
InBlock.gif            addInInstance 
= (AddIn)addInInst;
InBlock.gif            
if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
object []contextGUIDS = new object[] dot.gif{ };
InBlock.gif                Commands commands 
= applicationObject.Commands;
InBlock.gif                _CommandBars commandBars 
= applicationObject.CommandBars;
InBlock.gif
InBlock.gif                
// When run, the Add-in wizard prepared the registry for the Add-in.
InBlock.gif                
// At a later time, the Add-in or its commands may become unavailable for reasons such as:
InBlock.gif                
//   1) You moved this project to a computer other than which is was originally created on.
InBlock.gif                
//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
InBlock.gif                
//   3) You add new commands or modify commands already defined.
InBlock.gif                
// You will need to re-register the Add-in by building the CodeToolAddinSetup project,
InBlock.gif                
// right-clicking the project in the Solution Explorer, and then choosing install.
InBlock.gif                
// Alternatively, you could execute the ReCreateCommands.reg file the Add-in Wizard generated in
InBlock.gif                
// the project directory, or run 'devenv /setup' from a command prompt.
InBlock.gif
                try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Command command 
= commands.AddNamedCommand(addInInstance, "CodeToolAddin""CodeToolAddin""Executes the command for CodeToolAddin"true59ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
InBlock.gif                    CommandBar commandBar 
= (CommandBar)commandBars["Tools"];
InBlock.gif                    CommandBarControl commandBarControl 
= command.AddControl(commandBar, 1);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
catch(System.Exception /**//*e*/)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
InBlock.gif        
///     Receives notification that the Add-in is being unloaded.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='disconnectMode'>
InBlock.gif        
///      Describes how the Add-in is being unloaded.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='custom'>
InBlock.gif        
///      Array of parameters that are host application specific.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='IDTExtensibility2' />

InBlock.gif        public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
InBlock.gif        
///      Receives notification that the collection of Add-ins has changed.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='custom'>
InBlock.gif        
///      Array of parameters that are host application specific.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='IDTExtensibility2' />

InBlock.gif        public void OnAddInsUpdate(ref System.Array custom)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
InBlock.gif        
///      Receives notification that the host application has completed loading.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='custom'>
InBlock.gif        
///      Array of parameters that are host application specific.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='IDTExtensibility2' />

InBlock.gif        public void OnStartupComplete(ref System.Array custom)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
InBlock.gif        
///      Receives notification that the host application is being unloaded.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='custom'>
InBlock.gif        
///      Array of parameters that are host application specific.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='IDTExtensibility2' />

InBlock.gif        public void OnBeginShutdown(ref System.Array custom)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///      Implements the QueryStatus method of the IDTCommandTarget interface.
InBlock.gif        
///      This is called when the command's availability is updated
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='commandName'>
InBlock.gif        
///        The name of the command to determine state for.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='neededText'>
InBlock.gif        
///        Text that is needed for the command.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='status'>
InBlock.gif        
///        The state of the command in the user interface.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='commandText'>
InBlock.gif        
///        Text requested by the neededText parameter.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='Exec' />

InBlock.gif        public void QueryStatus(string commandName, EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(commandName == "CodeToolAddin.Connect.CodeToolAddin")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    status 
= (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///      Implements the Exec method of the IDTCommandTarget interface.
InBlock.gif        
///      This is called when the command is invoked.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param term='commandName'>
InBlock.gif        
///        The name of the command to execute.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='executeOption'>
InBlock.gif        
///        Describes how the command should be run.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='varIn'>
InBlock.gif        
///        Parameters passed from the caller to the command handler.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='varOut'>
InBlock.gif        
///        Parameters passed from the command handler to the caller.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param term='handled'>
InBlock.gif        
///        Informs the caller if the command was handled or not.
InBlock.gif        
/// </param>
ExpandedSubBlockEnd.gif        
/// <seealso class='Exec' />

InBlock.gif        public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            handled 
= false;
InBlock.gif            
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(commandName == "CodeToolAddin.Connect.CodeToolAddin")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    handled 
= true;
InBlock.gif                    MessageBox.Show(applicationObject.FullName);
InBlock.gif                    CreateProject();
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string RenderTemplate(string templateFile, string name, string destdir)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FileStream fs 
= new FileStream(templateFile, FileMode.Open, FileAccess.Read);
InBlock.gif            StreamReader sr 
= new StreamReader(fs,Encoding.Default);
InBlock.gif            
string strFile = sr.ReadToEnd();
InBlock.gif            sr.Close();
InBlock.gif            fs.Close();
InBlock.gif
InBlock.gif            strFile 
= strFile.Replace("[!output SAFE_NAMESPACE_NAME]", name);
InBlock.gif            strFile 
= strFile.Replace("[!output SAFE_CLASS_NAME]", name + "Class");
InBlock.gif
InBlock.gif            FileStream fs2 
= new FileStream(destdir + name + ".cs", FileMode.CreateNew, FileAccess.Write);
InBlock.gif            StreamWriter sw 
= new StreamWriter(fs2,Encoding.Default);
InBlock.gif            sw.Write(strFile);
InBlock.gif            sw.Close();
InBlock.gif            fs2.Close();
InBlock.gif
InBlock.gif            
return (destdir + name + ".cs");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void CreateProject()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string name = "TestDTEPro";
InBlock.gif            
InBlock.gif            Solution sol 
= applicationObject.Solution;
InBlock.gif
InBlock.gif            sol.Create(
"f:\\temp","TestDTE");
InBlock.gif            
string path = @"C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\DefaultDll.csproj";
InBlock.gif            Project theProject 
= sol.AddFromTemplate(path, "f:\\temp\\TestDTE", name, true);
InBlock.gif            VSProject myVSProject 
= (VSProject)theProject.Object;
InBlock.gif            
string templateFile = @"C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\CSharpDLLWiz\Templates\2052\file1.cs";
InBlock.gif            
string destination = @"f:\temp\TestDTE\";
InBlock.gif            
string filename = RenderTemplate(templateFile, "TestClass", destination);            
InBlock.gif            ProjectItem  theItem 
= myVSProject.Project.ProjectItems.AddFromFile(filename);
InBlock.gif            myVSProject.Project.Save(destination 
+ name + ".csproj");
InBlock.gif            myVSProject.Refresh();
InBlock.gif            sol.SaveAs(
@"f:\temp\newsolution.sln");
InBlock.gif            
//MessageBox.Show(theProject.FullName);        
InBlock.gif
            
InBlock.gif            EnvDTE.Window solutionExplorer 
= applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
InBlock.gif            solutionExplorer.Visible 
= true;
InBlock.gif            solutionExplorer.Activate();
InBlock.gif            
InBlock.gif            SolutionBuild sb 
= sol.SolutionBuild;
InBlock.gif            sb.BuildProject(sb.ActiveConfiguration.Name,theProject.UniqueName,
false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private _DTE applicationObject;
InBlock.gif        
private AddIn addInInstance;
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xiaotaoliang/archive/2006/08/31/491439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值