VB.NET makes an example of an external program
Summary
The original intention of writing this article stems from my "lazy" experience. Once I did some packages, there were about more than 60 different files, and each time I added a simple annotation, such as the use, version, etc. of this software, it is very inconvenient. Can there be a simple way, point a button, will automatically add a comment?
There are a lot of ways, today introducing .NET makes a small external program (add-in) to achieve an example of the above requirements.
Develop an add-in from VB.NET
Sometimes, developing an external program interacting with the development environment is a particularly meaningful thing, you can use a simple code to exchange very practical effects. VB.NET develops such add-in very simple, in the vs.net IDE environment, provides the development of add-in-in-in, a slight modification, you can get the effect you want.
Let's take a specific example:
Development example
First, open the development environment of VS.NET and create a new project. Browse the type of item on the left, see there is an extensibility project, click on the right to see there is a vs.net add-in:
We named this project for copyrightAddin, determined. See the scales of the wizard, click Next:
We choose the second item, use VB to create an add-on:
The second step tells us which procedures to choose as our "host", actually to load an environment for us to develop an external program, both of which are selected. Next:
Very simple, after entering the name and instructions of the external program, see the next step:
Select the first item, the default load this add-in, the next step:
This step is asked if you don't have an external program with a dialog. Choose Yes, you can customize information about the on the dialog, continue:
Finally, this picture shows some of your options to confirm that it is finished properly. There is an error, you can return to modify. Here, this wizard is over, our framework is also, we can add code.
At this time, look at the solution, the system is added to the SETUP project by default:
At this time, press F5, you can see "Effect": The system opens a new VS.NET development environment, let's open the tool menu, will make more CopyrightAddin menu items, click to click on what effect, because we still No write code.
Code:
For modularization, we create a SUB, CreateCopyrightFileForllvbfiles. Used to add a comment:
'Give each .vb file in the program Add to comment
Private sub createcopyrightfileForallvbfiles ()
Dim strremark as string
Strremark = "'This is a comment, there is Montaque Add" & ControlChars.crlf
Try
Dim Projects as array = ApplicationObject.ActiveSolutionProjects () 'All projects opened in the development environment
DIM theproject as envdte.project 'set up a project, indicating the current Project
If (Projects.length> 0) Then 'did not open Project, PASS is not bad.
TheProject = Projects.getValue (0) 'Get the current Project
DIM P AS ProjectItemFor Each P in Theproject.projectItems' Add a comment on all .vb files below the Project
If p.name.substring (p.Name.length - 3, 3) = ".vb" then
DIM theWindow as envdte.window
TheWindow = P.Open (constants.vsviewkindcode)
Dim objtextdoc as textdocument = theWindow.Document.object ("textdocument")
Dim objeditpoint as editpoint = objtextdoc.startpoint.createEditPoint
ObjeditPoint.Insert (strremark)
DIM STRFILENAME AS STRING = P.FileNames (0)
P.SAVEAS (StrfileName)
END IF
NEXT
END IF
Catch EE As Exception
Msgbox (Ee.tostring)
END TRY
End Sub
When the user calls the external procedure we write, there will be an Exec process, perform different processes based on the parameters passed to it, we join your own process:
Public Sub Exec (Byval CmdName As String, Byval ExecuteOption As VscommandexecOption, Byref Varources As Object, Byref Handled As Boolean) IMPEments IDTCOMMANDTARGET.EXEC
Handled = false
IF (ExecuteOption = vscommandexecoption.vscommandexecoptiondodefault) THEN
If cmdname = "CopyrightAddin.connect.copyright.copyright.copyrightAddin" THEN
CreateCopyrightFileForllvbfiles ()
Handled = TRUE
EXIT SUB
END IF
END IF
End Sub
OK, until now, run F5, you can see the actual effect. Is not it simple?
supplement
1. When it is released, we can make a setup project and you can install it.
2, after compiling, a registry file is generated in the directory where the project is located. When an external program is wrong, you will prompt you if you want to remove this add-on, and choose whether this add-in that you don't appear in the Tools menu, click this registry file to recover.
3, this is just a simple example, only for beginners to learn.