Hello!
I'm trying to get my program to automatically open a file when I doubleclick on it, I have set the file assosiation right but my code isn't working. I have read about the "Application.Run(new MyProgram());" but I have some problems.

When I do this:

public static void Main(string[] args)
        {
            string SelectedFile;
            if (args.Length > 0)
            {
                SelectedFile = Convert.ToString(args[0]);
                MessageBox.Show(SelectedFile); // Just to confirm it.
                Application.Run(new MyProgram(SelectedFile));
            }
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyProgram());   
        }

Thing is that I want to have futher use of the "SelectedFile" in my application.

I get the error "No overload for method 'MyProgram' takes '1' arguments" on the "Application.Run(new MyProgram(SelectedFile));" line.

Anyone know what to do?

What you need is a place in your MyProgram.cs class to store the command line arguments received in the constructor (Main).
Something like public string SelectedFile;

In the Main constructor, do this:

public static void Main(string[] args)       
{   string SelectedFile;            
     if (args.Length > 0)            
     {
        SelectedFile = Convert.ToString(args[0]);                       
        MessageBox.Show(SelectedFile); // Just to confirm it.               
     }
 
     MyProgram app = new MyProgram();
     app.SelectedFile = SelectedFile;
     Application.EnableVisualStyles();             
     Application.SetCompatibleTextRenderingDefault(false);              
     Application.Run(app);           
}

Then in your ...form_Load event, see if the value is null, or has a value. Take it from there.

Comments
joss
this helped me back when I didn't have rep power, and it helped me today again

my main from is midiparent and i want to use the file data in childfrom , in this way i cant get the fule to child from,
@scru , can u help me?

Comments
3 year-old thread
This article has been dead for over six months. Start a new discussion instead.