Question Getting argument from 2nd instance command line in single instance app

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
How do i get the argument from and 2nd instance in a single instace app? I tried to catch it in MyApplication_StartupNextInstance. But the e.commandLine or the AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData seems to be null. I use "Appname.appref-ms" 123 to send 123 as the argument but how do i recieve it?
 
I just tested and it worked exactly as expected for me. I created a project and made it single-instance. I then added this code:
VB.NET:
Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
    ' The following events are available for MyApplication:
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
            MessageBox.Show(String.Join(" ", e.CommandLine), "Startup")
        End Sub

        Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            MessageBox.Show(String.Join(" ", e.CommandLine), "StartupNextInstance")
        End Sub

    End Class
End Namespace
I then opened the Debug page of the project properties and entered "arg1 arg2" (without quotes) in the Command line arguments field. I then ran the project and the expected message popped up from the Startup event handler. I then right-clicked on the project in the Solution Explorer and selected Debug > Start New Instance and the expected message popped up from the StartupNextInstance event handler.
 
When i do it the way u did, it works. Then it must mean that when i do it in the command prompt, it failed. "Appname.appref-ms" 123. That doesnt work. How do i pass argument from the command prompt?
 
I'm not sure whether you can pass commandline arguments on to the application via an APPREF-MS file. If you create a LNK file, i.e. a standard shortcut, then you don't pass commandline arguments when you run that file and have them passed on to the application. The commandline arguments for the application are stored in the LNK file. I suspect that an APPREF-MS may be the same, although I'm not overly familiar with the file format.
 
This seems relevant:

 
When i do it the way u did, it works. Then it must mean that when i do it in the command prompt, it failed. "Appname.appref-ms" 123. That doesnt work. How do i pass argument from the command prompt?

You can retrieve the arguments with the code below.:
string value = Environment.GetEnvironmentVariable("ClickOnce_ActivationData_0");
See the full post here:
 
Back
Top