Results 1 to 3 of 3

Thread: {Resolved} File Association Arguments ?..?..?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    California
    Posts
    41

    Question {Resolved} File Association Arguments ?..?..?

    I am using vb.net exprtess 2005...
    I have the file type associated with my application, so when I double-click this file, it starts my application.
    Now I am wondering how the next step works...How do I recieve this file path?

    I know windows sends the file path to my application, but I don't know how to recieve it so I can open this file...Anyone know?

    Thanks...
    Last edited by Modderman; Jan 2nd, 2006 at 01:33 AM.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: File Association Arguments ?..?..?

    Here's a sample of how I did it in an application of mine. Basically, you start up using sub main in a module (making sure you change your startup object to Sub Main instead of your form), which accepts an args() parameter, which should be the filename passed in by windows. You would then need to have your application handle the passed in variable. The way I did it was modify the form constructor of the first form I loaded, running a certain procedure to open the file when a string was passed into the form...
    VB Code:
    1. 'module that includes the Sub Main (set project to startup on Sub Main)
    2. Module modStartup
    3.     Public Sub Main(ByVal args() As String)
    4.         MessageBox.Show(args(0)) 'just shows you the passed in string
    5.         If args.Length < 1 Then 'if less than one, run the form as normal
    6.             Application.Run(New frmMain(""))
    7.         Else
    8.             'pass in the string to the form constructor
    9.             Application.Run(New frmMain(args(0)))
    10.         End If
    11.     End Sub
    12. End Module
    13.  
    14. 'then in the form you run (frmMain in this example), have a certain procedure setup
    15. 'to run something if the string is not "", as in the below code:
    16.  
    17.     Private FilePathArg As String 'form level variable to hold the string passed in
    18.  
    19. 'now in the windows generated code section of your form, changing the New() constructor to accept a string parameter..
    20.     Public Sub New(ByVal FilePath As String) 'modified constructor
    21.         MyBase.New()
    22.         'This call is required by the Windows Form Designer.
    23.         InitializeComponent()
    24.         FilePathArg = FilePath 'sets form level string variable to the passed in variable
    25.         'Add any initialization after the InitializeComponent() call
    26.     End Sub
    27.  
    28. 'then in form load, handle the case when a string is passed in..
    29.      If FilePathArg <> "" Then 'if not "", then a string was passed in...
    30.          'run your code to open the file as you like, FilePathArg is the
    31.          'string containing the file path
    32.      End If
    Note that this was used in 2003, so there might be something a little different in the Windows generated code section, but everything should work just about the same...
    Last edited by gigemboy; Jan 1st, 2006 at 08:42 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: File Association Arguments ?..?..?

    Don't worry about passing arguments to your Main method. Just use the Environment.GetCommandLineArgs method:
    VB Code:
    1. Dim cla As String() = Environment.GetCommandLineArgs()
    2.  
    3.         'The first command-line argument is always the executable itself.
    4.         If cla.Length > 1 Then
    5.             MessageBox.Show("File to open: " & cla(1))
    6.         End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width