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?

Recommended Answers

All 5 Replies

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.

commented: this helped me back when I didn't have rep power, and it helped me today again +3
commented: joss +1

Ah got it!
Thank you for your help. Appriciate it.

THanks this helped me too!

A lot easier than I thought it would be...

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?

commented: 3 year-old thread -1

Really helpful thank you Jerry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.