其他分享
首页 > 其他分享> > vb.net VSIX开发 历遍项目 历遍文件

vb.net VSIX开发 历遍项目 历遍文件

作者:互联网

vb.net开发VS插件

想要操作一下 所有项目中的所有函数代码. 结果 查了大量的 微软参考文档,竟然没有发现

而网友提供的例子,多不是vb.net,而且没有操控代码的.基本都是ActiveDocument 插入注释等.

参考网友的代码 给 vb.net的朋友提供一个历遍项目 历遍文件的例子.

 

 

 1 Dim dtes As IEnumerable(Of DTE) = GetAllInstances()
 2 If dtes.Count() = 0 Then Return
 3 
 4 For Each dte1 As DTE In dtes
 5 If dte1.Solution.FileName.Contains("VSix测试用程序") Then
 6 If dte1 IsNot Nothing Then
 7 For Each items As EnvDTE.Project In dte1.Solution.Projects
 8 
 9 For Each item As ProjectItem In items.ProjectItems
10 
11 Dim prjItem As ProjectItem = item
12 If prjItem Is Nothing Then Return
13 Dim fcm As FileCodeModel = prjItem.FileCodeModel
14 If fcm Is Nothing Then Return
15 Dim ces As CodeElements = fcm.CodeElements
16 Dim cls As CodeClass = Nothing
17 Dim isStartEdit As Integer = 1
18 For Each ce As CodeElement In ces
19 If ce.Kind = vsCMElement.vsCMElementClass Then
20 cls = TryCast(ce, CodeClass)
21 For Each fs As CodeFunction In ce.Members
22 Dim cp As EditPoint = fs.StartPoint.CreateEditPoint
23 
24 isStartEdit = 1
25 While isStartEdit
26 Dim dt As String = cp.CreateEditPoint.GetText(cp.LineLength)
27 If dt.EndsWith("_" & vbCrLf & " ") OrElse dt.EndsWith("," & vbCrLf & " ") Then
28 cp.LineDown(1)
29 Else
30 cp.LineDown(1)
31 Exit While
32 End If
33 
34 isStartEdit += 1
35 
36 If isStartEdit > 20 Then Exit While
37 End While
38 
39 cp = fs.EndPoint.CreateEditPoint
40 cp.StartOfLine()
41 Next
42 
43 End If
44 Next
45 Next
46 item.Save()
47 
48 Next
49 
50 End If
51 End If
52 Next
53 Finally
54 End Try

 

 

 1 Private Shared Iterator Function GetAllInstances() As IEnumerable(Of DTE)
 2         Dim rot As IRunningObjectTable = Nothing
 3         Dim enumMoniker As IEnumMoniker = Nothing
 4         Dim retVal As Integer = GetRunningObjectTable(0, rot)
 5 
 6         If retVal = 0 Then
 7             rot.EnumRunning(enumMoniker)
 8             Dim fetched As IntPtr = IntPtr.Zero
 9             Dim moniker As IMoniker() = New IMoniker(0) {}
10             Dim punkObject As Object = Nothing
11 
12             While enumMoniker.[Next](1, moniker, fetched) = 0
13                 Dim bindCtx As IBindCtx = Nothing
14                 CreateBindCtx(0, bindCtx)
15                 Dim displayName As String = ""
16                 moniker(0).GetDisplayName(bindCtx, Nothing, displayName)
17                 Dim isVisualStudio As Boolean = displayName.StartsWith("!VisualStudio")
18 
19                 If isVisualStudio Then
20                     rot.GetObject(moniker(0), punkObject)
21                     Dim dte = CType((punkObject), DTE)
22                     Yield dte
23                 End If
24             End While
25         End If
26     End Function

 

标签:Dim,vb,End,While,Next,VSIX,Nothing,cp,历遍,vb.net源码
来源: https://www.cnblogs.com/MadeInChinese/p/16652342.html