PDA

Click to See Complete Forum and Search --> : making .txt default with my prog


DiGiTaIErRoR
Dec 11th, 1999, 05:26 PM
I want to be able to double click a txt file i.e. stuff.txt and instead of going to notepad it goes to my program. How can I set up my program to open the file and put it in text1.text?
Any help appreciated.

------------------
DiGiTaIErRoR

Dec 11th, 1999, 06:51 PM
the first step is to create a file association (i dont know how to do it through code, maybe theres an api that does it)

go to MY COMPUTER and click: view...options...filetypes...
choose to create a new file type, and have a look at some of the other entries to get an idea of how to set one up.

now the more tricky part, youll have to work out a way for the app to load the double-clicked file automatically when it loads. (Capturing the filename on loading!)



------------------

Wossname,
Email me: wossnamex@talk21.com :)

DiGiTaIErRoR
Dec 11th, 1999, 07:04 PM
I need to know how to get the file name then open it....
Thanks anyway....


------------------
DiGiTaIErRoR

QWERTY
Dec 11th, 1999, 09:38 PM
Here is the code to create file association:

'Make a new project. Add a module. To the form add a command button.

'Code:
'Add this code to the module:

Option Explicit

Public Const REG_SZ As Long = 1
Public Const REG_DWORD As Long = 4
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003

Public Const ERROR_NONE = 0
Public Const ERROR_BADDB = 1
Public Const ERROR_BADKEY = 2
Public Const ERROR_CANTOPEN = 3
Public Const ERROR_CANTREAD = 4
Public Const ERROR_CANTWRITE = 5
Public Const ERROR_OUTOFMEMORY = 6
Public Const ERROR_INVALID_PARAMETER = 7
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_INVALID_PARAMETERS = 87
Public Const ERROR_NO_MORE_ITEMS = 259

Public Const KEY_ALL_ACCESS = &H3F
Public Const REG_OPTION_NON_VOLATILE = 0

Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public 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, _
ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long

Public 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

Public 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 lpValue As String, ByVal cbData As Long) As Long

Public Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long


Public Sub CreateAssociation()

Dim sPath As String

'File Associations begin with a listing
'of the default extension under HKEY_CLASSES_ROOT.
'So the first step is to create that
'root extension item
CreateNewKey ".xxx", HKEY_CLASSES_ROOT


'To the extension just added, add a
'subitem where the registry will look for
'commands relating to the .xxx extension
'("MyApp.Document"). Its type is String (REG_SZ)
SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ


'Create the 'MyApp.Document' item under
'HKEY_CLASSES_ROOT. This is where you'll put
'the command line to execute or other shell
'statements necessary.
CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT


'Set its default item to "MyApp Document".
'This is what is displayed in Explorer against
'for files with a xxx extension. Its type is
'String (REG_SZ)
SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ


'Finally, add the path to myapp.exe
'Remember to add %1 as the final command
'parameter to assure the app opens the passed
'command line item.
'(results in '"c:\LongPathname\Myapp.exe %1")
'Again, its type is string.
sPath = "c:\LongPathname\Myapp.exe %1"
SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ

'All done
MsgBox "The file association has been made!"

End Sub


Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _
lType As Long, vValue As Variant) As Long

Dim nValue As Long
Dim sValue As String

Select Case lType
Case REG_SZ
sValue = vValue & Chr$(0)
SetValueEx = RegSetValueExString(hKey, _
sValueName, 0&, lType, sValue, Len(sValue))

Case REG_DWORD
nValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, _
0&, lType, nValue, 4)

End Select

End Function


Public Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)

'handle to the new key
Dim hKey As Long

'result of the RegCreateKeyEx function
Dim r As Long

r = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _
vbNullString, REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, 0&, hKey, r)

Call RegCloseKey(hKey)

End Sub


Public Sub SetKeyValue(sKeyName As String, sValueName As String, _
vValueSetting As Variant, lValueType As Long)

'result of the SetValueEx function
Dim r As Long

'handle of opened key
Dim hKey As Long

'open the specified key
r = RegOpenKeyEx(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_ALL_ACCESS, hKey)

r = SetValueEx(hKey, sValueName, lValueType, vValueSetting)

Call RegCloseKey(hKey)

End Sub

'Add this code to the command button:

Private Sub Command1_Click()
CreateAssociation
End Sub


------------------
Visual Basic Programmer
------------------
PolComSoft
You will hear a lot about it.



[This message has been edited by QWERTY (edited 12-12-1999).]

DiGiTaIErRoR
Dec 12th, 1999, 10:15 AM
I know how to create the association!
I needto know how to set itup so when the file is doubled clicked it opens my program(the association part)
then! what I NEED is to get the filename and open it in text1!!!

------------------
DiGiTaIErRoR

cjwares
Dec 12th, 1999, 02:18 PM
Hey, QWERTY, ok, thanx for the association code, but how do i make it change the icon as well?