Question File Association

schmeet

Member
Joined
Jul 19, 2010
Messages
8
Programming Experience
Beginner
Hi,

I have an application with a function to save data to .nmp files. And I have a function to open them.

When I published the application (A clickonce app, deployed using Studio Express), I set up file association.

Now this works and when I click on .nmp files it opens my app. However, I can't get my program to open the files. The way I understand it, the file path of the file which was clicked is sent via My.Application.CommandLineArgs(0).

So I have the following code to call my open file function during the form load event:

VB.NET:
If My.Application.CommandLineArgs.Count > 1 Then
            'Program was opened by a file extension
            LaunchFile = My.Application.CommandLineArgs(0)
            Call OpenFileFromPath(LaunchFile)
End If

However, nothing happens. Can anyone see anything particualrly wrong with this? Or does it just not work with clickonce apps?

Regards,
Pete
 
Have you actually checked what My.Application.CommandLineArgs.Count is? Have you read the documentation for that property?
The My.Application.CommandLineArgs property returns only the command-line arguments. This is different from the behavior of the CommandLine property, which returns the application name in addition to the arguments.
If there's one commandline argument, i.e. the path of the file you opened, what is My.Application.CommandLineArgs.Count going to be?

If things don't work as planned, don't just read your code. Run it and watch it in action. Don't assume what the values of variables, etc, will be. Run the code and check what the actual values are. If all our assumptions were true then it wouldn't be not working in the first place.
 
Thanks for your reply.

I see that I should be checking that it is greater than 0, not 1. However I tried this but it still doesn't work.
I normally make use of the debugging features of Visual Studio. However, i'm not sure how to debug the situation of the program being started by a file association.

Kindest Regards,
Pete
 
How is debugging it any different than running the app without any command line args? I debug all of my apps the same, both with and without args.
 
How is debugging it any different than running the app without any command line args? I debug all of my apps the same, both with and without args.

I'm sorry but I just dont understand what you mean.

When I press debug and run the app, it wont have any command line args due to it not being started via a file association.

I'm probably being dumb and I've never come across command line args before so dont really know about them but I don't understand how you debug the app with a command line arg that carries the file path of the file that started it (because the wasnt a file that started it).

Thanks again.
 
How is debugging it any different than running the app without any command line args? I debug all of my apps the same, both with and without args.

Ok, i've found the option in the debug menu for passing arguments to the app when its is run in debug mode.
It kind of works whe i type the arguments in that way.

however, when i publish the app nothing happens. So i changed my code to:

VB.NET:
If My.Application.CommandLineArgs.Count > 0 Then
            'Program was opened by a file extension
            MsgBox(My.Application.CommandLineArgs(0))
            LaunchFile = My.Application.CommandLineArgs(0)
            Call OpenFileFromPath(LaunchFile)
        Else
            MsgBox("No Command Line Arguments")
End If

And when i click one of my files, it opens the app and everytime gives me the message box saying No command line arguments. So for some reason when its published it isn't recieving command line arguments. Any ideas? i thought it may be a security issue, but I am running that app as full trust so it shouldn't be a problem.
 
So when you run the app with the file path typed in and everything in the startup args part of the project it works, but when you double click that same file it opens your app and doesn't work?
 
So when you run the app with the file path typed in and everything in the startup args part of the project it works, but when you double click that same file it opens your app and doesn't work?

Yeah basically. In debug it recognises that there is a command line arg, and tries to open the file but it thinks it doesn't exist (I think its something to do with there being a spce in the file name and it gets split to two lines). But when I publish it and double click one of my save files it runs the app but the app doesn't see any command line args.
 
Above the My.Application.CommandLineArgs.Count > 0 line put this: Messagebox.Show(My.Application.CommandLineArgs.Count.ToString), re-build and re-publish the app and open the file from windows
 
Ah, I missed the fact that this app was deployed using ClickOnce. The documentation for the My.Application.CommandLineArgs property says:
The My.Application.CommandLineArgs property provides read-only access to the current application's command-line arguments for applications that are not ClickOnce deployed.
In an application that is ClickOnce deployed, use the ActivationUri property of the My.Application.Deployment object to get the command-line arguments. For more information, see My.Application.Deployment Property.
Another example of why you should always read the documentation.

That said, I wonder whether Environment.GetCommandLineArgs works in a ClickOnce app. My guess would be 'no' but it would be worth testing.
 
Back
Top