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

Opening from Explorer

If I associate a file typ with my app, how do I set it up so that when I double click on a file of that type in Windows Explorer, my app loads it??
  • 6
  • 5
  • 2
  • +1
1 Solution
PhuzzyAuthor Commented:
Edited text of question.
Do you need help associating the type? Or getting the file to load it when it is double-clicked?
Also, if the problem is in asscoiating, do you want associate it manually or through code?

I can tell you how to do all three (associate through code, associate manually and open double-cicked file), but I need to know which one you need.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

PhuzzyAuthor Commented:
Associate through code, and open double-clicked file . . .
I answered this question on another forum yesterday, I'll post it here (this guy wanted to be able to make his program the default mp3 player)..

This make it so when you double-click on a file in explorer your program is executed alternatively with the filename as a parameter. Note that this add was meant for someone with low VB skills so it's kinda long..

"There's one way (copying code) or the better way to learn from an

 Open your registry editor program (regedit). Check the content of
 the HKEY_CLASSES_ROOT "directory". There u can see a
 "subdirectory" called ".mp3" this will be used to see what program
 should be run when the user runs a file with that extension. Look at
 what the "Standard Value" is, that's the description name for the
 extension.. Now there should be a "subdirectory" inside the
 HKEY_CLASSES_ROOT "directory" with the same name as the
 "Standard Value". There's some additional information about the
 file but the thingie u are interested in is the shell subdirectory. That
 contains a "open" directory which contains a "command" directory.
 The standard value in the shell\open\command tells what
 command line to run when a file is executed.

 And a example to help understand my very messy explanation:

 (Let's say u have Winamp registered with the extension .mp3)

 * Check the value of HKEY_CLASSES_ROOT\.mp3\(Standard
 Value) (if u have winamp it's "Winamp.File").

 * Then check the value of

 Now all u need to do is set that value to "YOUREXEFILENAME"
 and ur program will execute when u run a file with the extension

 If u need to have a parameter sent when u press the mp3 file u
 need to add %1 to that last "Standard Value", that will send u the
 parameters needed for your program to see what file is run. (Check
 the value of the string Command$ inside ur VB app).

 And one more thingie. All this I learned on my own just by opening
 my regedit program and used whatever I have "upstairs". So it
 doesn't hurt if u mess around a little. You can never do anything
 that a reinstallation can't fix ;) "
To add an association through code, you must create several keys and values.

The following are some of the registry API's and constants:

  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Boolean
End Type

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

Global Const REG_SZ = 1
Global Const HKCR = &H80000000
Global Const KEY_ALL_ACCESS = &HF003F

And some code(should work, untested):

Dim sa As Security_Attributes
x = RegCreateKeyEx(HKCR, ".YourExtension", 0, "", 0, KEY_ALL_ACCESS, sa, hRegKey, oc)
x = RegSetValueExString (hRegKey, "(Default)", 0, REG_SZ, "YourExtensionFile", Len("YourExtensionFile"))
x = RegCloseKey(hRegKey)
x = RegCreateKeyEx(HKCR, "YourExtensionFile", 0, "", 0, KEY_ALL_ACCESS, sa, hRegKey, oc)
x = RegSetValueExString (hRegKey, "(Default)", 0, REG_SZ, "Description of file type", Len("Description of file type"))
x = RegCloseKey(hRegKey)
x = RegCreateKeyEx(HKCR, "YourExtensionFile/shell", 0, "", 0, KEY_ALL_ACCESS, sa, hRegKey, oc)
x = RegSetValueExString (hRegKey, "(Default)", 0, REG_SZ, "Open", Len("Open"))
x = RegCloseKey(hRegKey)
x = RegCreateKeyEx(HKCR, "YourExtensionFile\shell\open\command", 0, "", 0, KEY_ALL_ACCESS, sa, hRegKey, oc)
x = RegSetValueExString (hRegKey, "(Default)", 0, REG_SZ, "Path to your app", Len("Path to your app"))
x = RegCloseKey(hRegKey)

The association should have been made by the above code.

.YourExtension is the .txt for a text file
YourExtensionFile is used to link the two main keys and for a text file is txtfile, but can be almost whatever you want
Description of file type is the same as "Text Document" for a text file.
The rest should be fairly easy to figure out.

If associated properly, when the file is double clicked it will be passed to your app as a command line. Use Command$ to retrieve it.
PhuzzyAuthor Commented:
Cheers, guys!
 P.S. Point taken about the messing about bit, that's my philosophy too, but I haven't had much time at the k/b of late. excuses, excuses!

Phuzzy are u still alive ?
did u accept any of these comments sent to u by those expert, if no i'll post the answer soon.

cheers vbmaster :)

PhuzzyAuthor Commented:
It hasn't given me the option to mark this Q as answered - i'll keep messing with it.
That's because no-one has posted an answer. Someone will have to post an answer before you can accept it. If me or VBMaster answered your question satisfactorily, then that one will post an answer for you to accept. If not, Pakos wishes to post an answer for the points.
That's fair.
thanx KDavid.
I thought so, but by my calendar it's been four days since he last posted to this page.
PhuzzyAuthor Commented:
I'll accept KDivad's answer if u post it ...
Sorry about the delay, as I said, not much k/b time.
My answer.

No problem about the delay, I think we were all just starting to wonder. I guess that means the code worked for you?

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.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now