954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Doubleclick to open file.

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?

MillStrike
Newbie Poster
13 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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.

JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

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

MillStrike
Newbie Poster
13 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

THanks this helped me too!

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

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

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?

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

Really helpful thank you Jerry!

PierlucSS
Junior Poster
187 posts since Feb 2010
Reputation Points: 65
Solved Threads: 30
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You