导入和导出任务列表

Visual Studio的IDE可以用任务列表保存我们在项目进展期间的一些信息。我个人也很喜欢用。但默认情况下是没有办法将这些信息保存起来的,更谈不上我们把它导出其他的格式了。

image

为此,我专门写了两个宏去做这个事情

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.IO
Imports System.Xml
'''''''''''''''''''''''''''''''''''''''''''''''''''''
'导入和导出用户定义任务列表的宏模块
'作者:陈希章
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Module TaskManagementModule
    Public Class WinWrapper
        Implements System.Windows.Forms.IWin32Window

        Overridable ReadOnly Property Handle() As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
            Get
                Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd)
                Return iptr
            End Get
        End Property
    End Class
    <STAThread()> _
    Public Sub ExportTaskList()
        Dim winptr As New WinWrapper
        Dim dialog As New SaveFileDialog

        dialog.Filter = "任务列表(*.XML)|*.XML"
        dialog.Title = "保存为..."
        If dialog.ShowDialog(winptr) = DialogResult.OK Then
            Dim fs As New FileStream(dialog.FileName, FileMode.OpenOrCreate)
            Dim xw As New XmlTextWriter(fs, System.Text.Encoding.UTF8)
            xw.WriteStartDocument()
            xw.WriteStartElement("TaskList")
            For Each task As TaskItem In DTE.ToolWindows.TaskList.TaskItems
                xw.WriteStartElement("TaskItem")
                xw.WriteStartElement("Category")
                xw.WriteCData(task.Category)
                xw.WriteEndElement()

                xw.WriteStartElement("SubCategory")
                xw.WriteCData(task.SubCategory)
                xw.WriteEndElement()

                xw.WriteStartElement("Description")
                xw.WriteCData(task.Description)
                xw.WriteEndElement()

                xw.WriteEndElement()
            Next
            xw.WriteEndElement()
            xw.Close()

        End If
    End Sub
    <STAThread()> _
    Public Sub ImportTaskList()
        Dim winptr As New WinWrapper
        Dim dialog As New OpenFileDialog

        dialog.Filter = "任务列表(*.XML)|*.XML"
        dialog.Title = "打开任务列表"
        If dialog.ShowDialog(winptr) = DialogResult.OK Then

            Dim doc As New XmlDocument()
            doc.Load(dialog.FileName)

            For Each node As XmlNode In doc.SelectNodes("TaskList/TaskItem")
                Dim category As String = node.SelectSingleNode("Category").InnerText
                Dim subcategory As String = node.SelectSingleNode("SubCategory").InnerText
                Dim description As String = node.SelectSingleNode("Description").InnerText

                DTE.ToolWindows.TaskList.TaskItems.Add(category, subcategory, description)
            Next

        End If
    End Sub
End Module

以上代码只是一个示范,还可以进一步细化。因为任务有好几种类型:用户任务,快捷方式(书签),注释(TODO)等等

 

导出的XML文件大致如下

image

最后,我可以把这两个宏添加到解决方案的快捷菜单中去

image

原文地址:https://www.cnblogs.com/chenxizhang/p/1271980.html