指定包含 Sub Main 过程的类或模块。Specifies the class or module that contains the Sub Main procedure.




必需。Required. 类或模块的名称,其中包含在程序启动时要调用的 Sub Main 过程。The name of the class or module that contains the Sub Main procedure to be called when the program starts. 此格式可以是 -main:module 或 -main:namespace.module 。This may be in the form -main:module or -main:namespace.module.


创建可执行文件或 Windows 可执行程序时,请使用此选项。Use this option when you create an executable file or Windows executable program. 如果省略“-main” 选项,编译器将在所有公共类和模块中搜索有效的共享 Sub MainIf the -main option is omitted, the compiler searches for a valid shared Sub Main in all public classes and modules.

有关 Main 过程的各种形式的讨论,请参阅 Visual Basic 中的 Main 过程See Main Procedure in Visual Basic for a discussion of the various forms of the Main procedure.

如果 location 是从 Form 继承的类,则编译器将提供一个默认的 Main 过程,该过程在类没有 Main 过程的情况下启动应用程序。When location is a class that inherits from Form, the compiler provides a default Main procedure that starts the application if the class has no Main procedure. 这使你可以在开发环境中创建的命令行上编译代码。This lets you compile code at the command line that was created in the development environment.

' Compile with /r:System.dll,SYSTEM.WINDOWS.FORMS.DLL /main:MyC
Public Class MyC
    Inherits System.Windows.Forms.Form
End Class

在 Visual Studio 集成开发环境中设置 -mainTo set -main in the Visual Studio integrated development environment

  1. “解决方案资源管理器” 中选择一个项目。Have a project selected in Solution Explorer. 在“项目”菜单上,单击“属性” 。On the Project menu, click Properties.

  2. 单击“应用程序” 选项卡。Click the Application tab.

  3. 请确保未选中“启用应用程序框架” 复选框。Make sure the Enable application framework check box is not checked.

  4. 修改“启动对象” 框中的值。Modify the value in the Startup object box.


下面的代码编译 T2.vbT3.vb,指定将在 Test2 类中找到 Sub Main 过程。The following code compiles T2.vb and T3.vb, specifying that the Sub Main procedure will be found in the Test2 class.

vbc t2.vb t3.vb -main:Test2  

请参阅See also