Question

I have a windows forms application with a custom file extension set up. I am able to save data to my file, and when I double-click my saved file from Windows, it launches my application.

I have not, however, been able to get the name of the file I clicked on to read in its data. Everything seems to tell me args[0] should be the exe (as I'm seeing), args[1] would be the next parameter (probably what i'm looking for; the file name I clicked on) but args.Length is always just 1, whether I open the exe directly or click on a text file that launches the exe, I never have the file name I clicked on.

Edit (resolved; ish): OK, finally have a more specific issue nailed down. My application was deployed with ClickOnce, and I set up all the file associations through the windows forms application properties. When I right-click and view the properties of my saved custom file, it says "Opens with: ClickOnce Application Deployment Support Library" and not my application name. If I change the default to open with my .exe, magically it has the correct arg values (the exe, followed by the file name I clicked on).

Était-ce utile?

La solution

You can't access command line arguments for ClickOnce applications directly. To get to them, I used the following, modified a bit from here:

System.Runtime.Hosting.ActivationArguments args = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
if (args.ActivationData != null)
{
    foreach (string commandLineFile in AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData)
    {
        MessageBox.Show(string.Format("Command Line File: {0}", commandLineFile));
    }
}

This gave me the file name I clicked on. Hoorah.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top