Windows Develop Bookmark and Share   
 index > Windows Forms General > Recieving an Open File request from Windows
 

Recieving an Open File request from Windows

I have an application that I'm developing that uses a custom file format for storing users files in and I'm wondering how to set my application up so that when a user opens a file of this type the application runs and opens the file.

I've added the filetype to Windows list of file extensions and linked its open with to the appropriate application so when a file is opened the application runs but I cannot work out how to get the program to detect that its been run in order to open a file and consequently open the file. 

How can I find out whether my application has been run to open a file and how can I find out the information I need in order to open the file (ie. the filename and path)?

Any help, advice, links to articles etc would be appreciated.
MigrationUser 1  Sunday, December 21, 2003 2:19 PM
Take a look at Environment.CommandLine. It should contain the necessary parameters.  If memory serves correctly, handle this in the YourMainForm.Open().
MigrationUser 1  Monday, December 22, 2003 2:56 PM
In addition, since all WinForms applications start as the result of a static Main being run, you can always grab the parms right off of the args collection.  Something like:


[STAThread()]
private static void Main(string[] args) {
    MySpecialForm frm = new MySpecialForm();
    foreach(string arg in args) {
        switch(arg) {
            case "-switch";
                frm.Switch = true;
                break;
            default:
                frm.AddFileName(arg);
                break;
        }
    }

    Application.Run(frm);
}


This gives you the ability to make your from class what I would call a runnable dialog window.  You could literally re-use this form in any application, because it now has properties and methods you can call to set it up just like any other workhorse class.  All of the init work in this case happens in the Main method, but if you were using this instead as a tool, you might imagine launching an Open File dialog and custom UI to set the switches then using the results to call this same form.

I highly recommend against using the Environment.CommandLine in any Form overloads since they are then dependent on being run in a very specific way and lose a LOT of their reusability.
MigrationUser 1  Monday, December 22, 2003 5:44 PM

You can use google to search for other answers

Custom Search

More Threads

• Get Mouse pointer position
• Getting the reference to the containing Form for a component
• ToolStripComboBox setting SelectedIndex to -1 doesn't work
• How do I display two Windows Forms Simultaneously (perfer the CLR CLI version of Code) (C++)
• TreeView Levels - Not going deep enough
• Display ANSI characters?
• Creating an exe without using a Form
• Multiple applications printing
• Esc in maskedtextbox
• Main Form not showing up in taskbar