Gabe Anzelini
Gabe Anzelini

Reputation: 233

Filename not being passed to main

I have an app and I have a file type associated with it. When I doubleclick the file it opens my app but the filename/path never gets passed to my app. It does work if I drag the file over the icon, however. Here is the main():

    static void Main(string[] filenames)
    {
        Form1 form = null;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        form = new Form1();
        if (filenames != null && filenames.Length > 0)
            form.FileName = filenames[0];
        Application.Run(form);
    }

filenames.length comes up 0 every time (unless I drag/drop the file onto the app)

Upvotes: 1

Views: 620

Answers (3)

Ruben
Ruben

Reputation: 15525

Does your file association contain a %1 or "%L" as part of the command line? This is required to pass the file name to your application. ("%L" means the full, long file name, %1 is the shortened 8.3 file name.)

The reason your application does receive the file name when you drop the file on the application icon, is because Windows doesn't use a file association here. In fact, it will work for any program, with and without file association. In these cases it guesses that you probably want to start the application with the file's name as an argument.

Upvotes: 2

swatkat
swatkat

Reputation: 5015

You need to have %1 or %L in file association settings to "tell" Windows Explorer to pass filename to your application when double-clicked on the file. An example:
http://hypography.com/forums/tutorials-and-how-tos/15876-windows-file-association-tutorial.html

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273274

You should try

AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData

is also is a string[]

Upvotes: 1

Related Questions