简体   繁体   中英

Windows Forms equivalent of WPFs OnStartup event

protected override void OnStartup(StartupEventArgs e)

I want the equivalent of this event for Windows Forms.

I'm making a image viewing application. My application is one of the default programs that opens a .JPG. So how can i get the path of the file

In the file Program.cs , there should be following method:

static void Main(string[] args)

args contains the parameters passed to your application, eg the file that should be opened.

If you just need the path of the file started the process you can go about this

var path = Assembly.GetEntryAssembly().Location;

To get the path of file association clicked you need to look at the command line args

var pathOfFileAssociation = Environment.GetCommandLineArgs()[1] ;

You can do it like this:

private void Form1_Load(object sender, System.EventArgs e)
{
    //this gives you the path of the executing assembly
    MessageBox.Show(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM