不知道你有没有遇到这种情况: 当要找一些以前做的代码做参考的时候,总是在文件夹里翻来翻去,光看项目名字也还是想不起来重要的代码究竟在哪个工程里。因此,自己开发个AddIn来方便进行本地的代码管理,通过添加本地的某个文件夹管理下面的所有的Solution。在AddIn里,可以快速切换Solution,不用在资源浏览器里费劲找,同时通过Tag管理可以快速过滤,方便定位想要的代码。
请猛击这里下载:
VS2008_WorkspaceAddIn
VS2010_WorkspaceAddIn
安装 AddIn,只要将 .dll 和 .AddIn 文件拷贝到下面的路径,启动VisualStudio就可以了。
VS2008: [/Documents and Settings/My Documents/Visual Studio 2008/Addins]
VS2010: [/Documents and Settings/My Documents/Visual Studio 2010/Addins]
源码下载:http://download.csdn.net/detail/fangxinggood/3669102
【功能介绍】
当然也支持VS2010
Workspace的数据都将保存在AddIn.dll的同级目录下的 WorkspaceAddIn.data.xml 中,
它是通过Xml序列化/反序列化进行的存取的。这里就不详细介绍了。
接下来简单介绍一下AddIn开发:
1. 创建一个AddIn工程(Other project types –> Extensibility),如下图:
外接(插件)程序是一些可以为我们节省时间和精力的应用程序,可附加到 Visual Studio 集成开发环境 (IDE) 中使用。外接程序是 COM 对象,它们实现 IDTExtensibility2 接口,并通过 EnvDTE和 EnvDTE80 程序集中包含的核心自动化对象模型与 IDE 通信(EnvDTE: 支持VS2003以前的IDE, EnvDTE80:支持VS2005以后的IDE)。工具栏模型包含在 Microsoft.VisualStudio.CommandBars 中。使用到的主要对象就是DTE对象,利用它我们能编程控制在Visual Studio中的任何东西,从工具栏,装卸工具窗口,甚至编辑文件和初始化编译。
2. 实现 Connect.cs 的 OnConnection 方法
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { // Cache the DTE and add-in instance objects _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Only execute the startup code if the connection mode is a startup mode if (connectMode == ext_ConnectMode.ext_cm_AfterStartup || connectMode == ext_ConnectMode.ext_cm_Startup) { try { // Declare variables EnvDTE80.Windows2 toolWins; object objTemp = null; // The Control ProgID for the user control string ctrlProgID = "WorkSpaceAddin.UI.WorkSpaceWindow"; string ctrlProgName = "WorkSpaceAddin"; // This guid must be unique for each different tool window, // but you may use the same guid for the same tool window. // This guid can be used for indexing the windows collection, // for example: applicationObject.Windows.Item(guidstr) string guidStr = "{BCC6C29D-4B3D-4ce2-8715-7EB9279D6440}"; // Get the executing assembly... System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); // Get Visual Studio's global collection of tool windows... toolWins = (Windows2)_applicationObject.Windows; // Create a new tool window, embedding the WorkSpaceWindow control inside it... _windowToolWindow = toolWins.CreateToolWindow2(_addInInstance, asm.Location, ctrlProgID, ctrlProgName, guidStr, ref objTemp); // Pass the DTE object to the user control... _workspaceWindow = (WorkSpaceWindow)objTemp; _workspaceWindow.DTE = _applicationObject; // and set the tool windows default size... _windowToolWindow.Visible = true; // MUST make tool window visible before using any methods or properties, // otherwise exceptions will occurr. //toolWin.Height = 400; //toolWin.Width = 600; _windowToolWindow.Height = 400; _windowToolWindow.Width = 280; } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } } }
OnConnection方法的第一个参数application就是DTE2的实例,而DTE2.Windows就是当前IDE里的视窗集合(比如:Output视窗,Debug视窗等),利用 DTE2.Windows.Create 方法可以创建一个工具视窗,并通过自定义的工具窗体的自定义的属性: _workspaceWindow.DTE = _applicationObject; 将DTE2引用传给作为AddIn的窗体,以控制IDE。
3. 实现自定义的工具窗体的应用——操作IDE
WorkspaceAddIn里主要的一些操作IDE的方法:
1) 打开一个Solution: DTE.Solution.Open(path);
2) 切换Solution,让IDE弹出保存提示框: vsPromptResult result = DTE.ItemOperations.PromptToSave;
3) 获取当前Solution:
System.Array projs = DTE.ActiveSolutionProjects as System.Array;
foreach (Project prj in projs)
{
MessageBox.Show(prj.Name);
}
4) 关于DTE2的编程模型请参考: DTE2的MSDN
4. 关于 AddIn 的配置文件 - xxx.AddIn
<?xml version="1.0" encoding="UTF-16" standalone="no"?> <Extensibility xmlns="http://schemas.microsoft.com/AutomationExtensibility"> <HostApplication> <Name>Microsoft Visual Studio</Name> <Version>9.0</Version> </HostApplication> <Addin> <FriendlyName>WorkspaceAddIn</FriendlyName> <Description>WorkspaceAddIn For Source Manage</Description> <Assembly>WorkspaceAddIn.dll</Assembly> <FullClassName>WorkspaceAddIn.Connect</FullClassName> <LoadBehavior>0</LoadBehavior> <CommandPreload>0</CommandPreload> <CommandLineSafe>0</CommandLineSafe> </Addin> </Extensibility>
<Assembly />节指定了AddIn实现的dll的路径,
<LoadBehavior />配置了加载AddIn的行为:
值 | 说明 |
0 | IDE启动的时不加载必须手动加载 |
1 | IDE启动时自动加载 |
4 | IDE从命令行启动时加载 |
关于AddIn配置的详细信息请参考: MSDN(Add-In Registration)
5. 另外,在IDE中手动管理AddIn
LoadBehavior, CommandPreLoad 的行为同样能在IDE中手动配置: IDE的工具栏中 Tools > Add-In Manager
还需要完善的:
1) Workspace 只加载 .sln 的文件,对于只有 .csproj 没有 .sln 的工程还没设计加载。
2) WebSite 还没有设计加载
3) 工程类型的判断上还存在问题
-- 全文完 --