locked
command line arguments RRS feed

  • Question

  • Hi all

    I can successfully use the command-line arguments by the code below

    Dim myFile As String
    If My.Application.CommandLineArgs.Count > 0 Then
    myFile = My.Application.CommandLineArgs(0)
    'some lines of code
    End If
    
    when a user double clicks on a file associated with my application the application starts, process the command and perform some operations but while the application is running and user again double clicks a file it starts another instant of the program how can i make the program to process the command line argument without making the application single instance.

    Friday, July 15, 2011 4:52 PM

Answers

  • Sorry I missed the bit about prcessing the arguments a second time - as Andrew has said you have two choices.

    The problem is that when the file is clicked a second time you already have a new instance running and need to pass the arguments across to the first instance. You could write the argumenst to a file and read that in , write to a database record and read that in but I can't help thinking it would be easier just to let your users work with a single user interface - display a list of file icons on your form and let them click there instead of directly from windows.


    David Severn
    • Proposed as answer by Andrew B. Painter Wednesday, July 27, 2011 1:59 PM
    • Marked as answer by Faraz Zone Thursday, July 28, 2011 3:01 PM
    Wednesday, July 27, 2011 7:40 AM

All replies

  • You need the change the application property from Multiple Instance to Single Instance.

     

    Here is a good article on how to do it: http://msdn.microsoft.com/en-us/library/8fz4ssw2%28v=vs.90%29.aspx

    Here is a very good tutorial on single instance with command line: http://www.codeproject.com/KB/vb/singleinstance.aspx

    Thanks.


    If my post answers your question then mark as answer.
    Friday, July 15, 2011 7:09 PM
  • At the end of my first post i have written

    without making the application single instance.

    any way thanks for the reply.

    Saturday, July 16, 2011 12:17 PM
  • I think you would need some sort of flag - an entry in the registry or write a file when the app starts - when it ends delete the file. When the app starts check to see if the registry entry or file exists and if so just exit 


    David Severn
    Saturday, July 16, 2011 12:47 PM
  • Hello David

    Sorry for replying so late

    I have done as you said but i am  stuck with the problem to read the second argument, I have the below line in a timer but it never works

    myFile = My.Application.CommandLineArgs(1)
    

    thanks for the help


    Faraz
    Monday, July 25, 2011 5:18 PM
  • I think what you want to do here is:

    User double-clicks, application opens and processes arguments.

    While application is open, user double-clicks, application does not open new instance but instead processes new command with arguments.

    You have exactly two options here; either make the application single-instance or else remove the Application Framework altogether and build a custom beast... the custom beast then basically has to mimic the behavior of the Single Instance application.  If you seriously plan to chase the second beast, the best advice I can give you is to make sure you get your foot squarely in front of the barrel.

    Once the application is set to Single Instance, you use the StartupNextInstance event in the My.Application class to process the next command passed by a re-launch operation on the user's end.

     


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Monday, July 25, 2011 6:53 PM
  • Sorry I missed the bit about prcessing the arguments a second time - as Andrew has said you have two choices.

    The problem is that when the file is clicked a second time you already have a new instance running and need to pass the arguments across to the first instance. You could write the argumenst to a file and read that in , write to a database record and read that in but I can't help thinking it would be easier just to let your users work with a single user interface - display a list of file icons on your form and let them click there instead of directly from windows.


    David Severn
    • Proposed as answer by Andrew B. Painter Wednesday, July 27, 2011 1:59 PM
    • Marked as answer by Faraz Zone Thursday, July 28, 2011 3:01 PM
    Wednesday, July 27, 2011 7:40 AM