[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Code to open file automatically by association

I am looking for the code to open an associated file automatically. For example, I have created a VB application which opens and read files with the extension .PAG

When a user double-clicks on a file with the extension of .PAG in Windows Explorer, I want the VB application to open that particular file after starting.
1 Solution
If its a new extention, simply double click it in windows explorer.
windows asks what to use.
browse to your app, check the "always use this program" and OK.

If you want to do this programattically, use the api viewer to add registry funcs.
Go HKey_CLASSES_Root and find your key(s), simply emulate this.

mcdonald_gAuthor Commented:
The code I am looking for is strictly VB syntax. I already have the association set in Windows Explorer.

I need the VB code to open the file that was double-clicked in Explorer.

Right now when I double-click on a filename with an extension of .PAG, my VB app launches but it doesn't open the file. I need to plug in code to open the file after the app starts.
You will need to get the command line arguments in the form create of your application. This will give you the filename of the double clicked file in windows explorer. Thus this can be used to read in the file.

When applications are double clicked in windows explorer they launch the associated file passing the filename of the double clicked file.

The 'Command' function returns what ever command line parameters have been passed to your application. In this case there will be one which is the file name.

Private Sub Form_Load()
Dim FileName As String
 FileName = Command
 MsgBox FileName
 '....load the text file given file name
End Sub
When the program starts, check the value of the Command function.  It should be the filename if your program was started from clicking on an associated file.
mcdonald_gAuthor Commented:
Thanks a lot! It worked.


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now