Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get arguments in a form application?

Tags:

c#

arguments

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?

like image 695
Moon Avatar asked Aug 06 '09 21:08

Moon


2 Answers

Environment.GetCommandLineArgs

like image 83
David Avatar answered Oct 03 '22 00:10

David


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.

like image 25
Allen Rice Avatar answered Oct 02 '22 23:10

Allen Rice