Passing command line arguments to windows app

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nsteiner
    New Member
    • Dec 2008
    • 25

    Passing command line arguments to windows app

    Hi all
    I am trying to differ between launching an application from 'startup' or from the desktop shortcuts.
    I thought of adding a command line argument to the shortcuts through the msi installer but i'm not sure how to do it.
    The next step would be reading the argument and acting accordingly.

    Can someone please help ?

    Thanks in advance.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Isn't "from 'startup' " a shortcut in Documents\mydoc uments\startmen u\programs\star tup ?
    So you're trying to differentiate between *which* shortcut was used to launch?

    Comment

    • cloud255
      Recognized Expert Contributor
      • Jun 2008
      • 427

      #3
      Hi

      Do you want you want to create a shortcut to your application using the command prompt or do you want the application to launch at startup using the command promt?

      Comment

      • nsteiner
        New Member
        • Dec 2008
        • 25

        #4
        Hi tlhintoq and cloud255
        Thanks for your replys, I will try to answer:
        tlhintoq, yes, exactly. I am trying to differentiate between which shortcut was launched. This is for a simple reason:
        If the app is launched on startup, I would like it to be minimizes to the system tray, if not, I would like it to be in a 'Normal' state.

        cloud255, none.
        What I have already are 2 shortcuts created by the MSI installer.
        1 in the startup folder and one on the desktop.
        I would like to pass some sort of argument from these shortcuts to the application in order to control the app size.

        Thanks
        nsteiner

        Comment

        • cloud255
          Recognized Expert Contributor
          • Jun 2008
          • 427

          #5
          Hi,

          I found a solution, but it might not be optimal. Anyways, the problem here is that a shortcut always points to the executable file within a certain directory, so the Application and Environmental variables always point to the actual path of the .exe

          What I did was change the Main procedure within the program.cs file
          form:

          Code:
          static void Main()
          to

          Code:
          static void Main(string[] mArgs)
          This allows your application to access the command line arguments supplied on application launch.

          I also altered the Application.Run () too look like below:

          Code:
          Application.Run(new Form1(mArgs));
          This allows your form1's constructor to access the command arguments and perform an action based on that.

          Now you can change the target property of the shortcut to be something like:

          Code:
          "C:\MyApp.exe" short
          Each space in the target path specifies a new element within the mArgs array I declared. You can now test if mArgs[0] has a value and what that value is and act accordingly.

          Hope this helps

          Comment

          • nsteiner
            New Member
            • Dec 2008
            • 25

            #6
            Originally posted by cloud255
            Now you can change the target property of the shortcut to be something like:
            Code:
            "C:\MyApp.exe" short
            Thanks a lot, cloud255.
            This is exactly what I need.
            Just one more question.
            I want the shortcut arguments to be written within the installer, so that I wouldn't have to modify the actual shortcuts manually.
            Is that possible?
            I can see an 'argument' property for the shortcut in the installer.
            Is this it ?

            Thanks

            Comment

            • cloud255
              Recognized Expert Contributor
              • Jun 2008
              • 427

              #7
              Hi,

              Yes, the arguments property will allow you to specify additional arguments for the shortcut. Remember to assign different argument values for shortcuts which will cause the application to run in the system tray to those which will cause the app to run normally.

              Good luck

              Comment

              Working...