在 VB.NET 应用程序启动时运行代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21945070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run code when VB.NET application starts
提问by Goku
How do i make code run when an application starts up?
如何在应用程序启动时运行代码?
I want to open up a config file and parse it and then set up the user where they left off last. I built the methods for reading the config file but I dont know how to make those methods run automatically when the program starts up. Is there some Start() method that i need to look for? In an android app for instance there are Create() Start() and Resume() methods that I can use to call code when the app starts.
我想打开一个配置文件并解析它,然后在他们最后离开的地方设置用户。我构建了读取配置文件的方法,但我不知道如何让这些方法在程序启动时自动运行。我需要寻找一些 Start() 方法吗?例如,在一个 android 应用程序中,我可以使用 Create() Start() 和 Resume() 方法在应用程序启动时调用代码。
回答by Dayan
If your application is running in a Console, then simply put your code inside the Main method.
如果您的应用程序在控制台中运行,那么只需将您的代码放在 Main 方法中。
Module Module1
Sub Main()
' Say hi in VB.NET.
Console.WriteLine("Hello world")
End Sub
End Module
If your code is running in a WinForm then put it on Form_Loadevent. Note: Replace Form1with the name of your Form.
如果您的代码在 WinForm 中运行,则将其放在Form_Load事件上。注意:替换Form1为您的表单名称。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
If your code is WPF then subscribe to the Loaded event:
如果您的代码是 WPF,则订阅 Loaded 事件:
public MyWindow()
{
Loaded += MyWindow_Loaded;
}
private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
// do work here
}

