tags:

views:

116

answers:

2

Hi,

I can find many examples on how to get arguments in a console application, but I can't seem to find an example of how to get arguments in a windows form application.

I would like to following things.

  1. whenever I open a jpg file, windows launches my application.
  2. I would like to know path and name of the jpg file from my application.

How do i do that?

+6  A: 

Environment.GetCommandLineArgs

David
A little cleaner than my method and I can't find anything negative about it. You may want to go with this for simplicity sake
Allen
Hmm, I don't agree on this being cleaner than using a method parameter. The latter gives you a fixed point where to decide how your application should behave at startup, rather than parsing the command line in some arbitrary spot.
Thorarin
+4  A: 

Open up program.cs, on a file > new > winform project, you'll get

static class Program
{
    [STAThread]
    static void Main()
    {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Form1());
    }
}

change this to

static class Program
{
 [STAThread]
 static void Main(string[] args)
 {
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
 }
}

Now its just like the console apps, you'd access them via args.

Even if you don't go with this option, you should be aware of how the win form app is initialized :) This way, you could run different forms or not run a form at all.

Allen