File Types
Hi,
I have an application that is able to save and load data in text form using a '.sdq' file type that I made up. I have a couple of questions:
- how can I make the app check when it is launched whether .sdq is assigned to any other application, and if not, assign it to my application (or prompt the user) ?
- I've assigned this file type to my application manually, so that now if I double-click on a .sdq file in the file explorer it will spawn a new version of the app but doesn't load the file into the app - when the app is spawned, how can I make it call the load subroutine to load the file that spawned it ?
Cheers,
Stu
Well you'll need to add a few registy settings. This is how I set my own file types.
Public Sub MyFileExt()
Dim s_App_Location As String = Application.ExecutablePath
My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\.sdq", Nothing, "Your App Name", 1) 'You set and assiocate your file type.
My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\Your App Name", Nothing, "Your App Description", 1) 'When you hover over the file you'll get a little box with some info of that file. The description is one of them.
My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\Your App Name\DefualtIcon", Nothing, s_App_Location, 1) 'This will assign the file type an icon, in this case the icon of the program. You can change the s_App_Location to what ever the icon you want. s_App_Location in the location of the icon or program.
My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\Your App Name\shell\open\command", Nothing, s_App_Location & " %1", 1) 'This will launch your program when you double click the file, you MUST remember the %1 because that will pass the file location to the program. If you for get this ther program will run but your file will not load
End Sub
This code is off the top of my head so if it contains error feel free to correct me. But I think for the most part this will work. I hope this help you a bit.
OK, your correct in changing the association of the program so that for file extension files they are pointing to you application but you also need to take in a command line parameter for you application which will be used to load the contents of the file into you application when provided.
The following shows a simple Module that contains a module with a sub main that has a parameter of cmdargs() as string. This will enable you to call your application with a filename after it and it should load the contents into the application.
So If my application was called Foo and I had set up a file associate for .asb to this application I could go to command windows and type Foo.exe C:\test.asb and it would load the contents of test.asb into my foo application.
This is in essence what explorer is doing - it is calling the application associated with the extension and calling it providing the filename as a first command line parameter.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Module Module1
Sub main(ByVal cmdargs() As String)
Dim frm As New Form1
Dim strFilename As String
If UBound(cmdargs) >= 0 Then
strFilename = cmdargs(0)
frm.TextBox1.Text = My.Computer.Filesystem.ReadAllText(strFilename)
Else
frm.TextBox1.Text = ""
End If
frm.ShowDialog()
End Sub
End Module
Try it with a simple form and see how this works and then you can incorporate it into you application.
Without this - clicking the file will start you application but the contents will not be loaded.
thanks both, I'll try those over the weekend. Couple of questions though:
Spotty - should there have been a call to main(cmdargs) from your sub Form1_Load ? What do I pass as argument to cmdarg ? I presume it's sender.something or e.something ?
Wellnow - I'd noticed that the app in the Startmenu and explorer doesn't use the icon I set in VB Express - is this fixed by your code My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\Your App Name\DefaultIcon", Nothing, s_App_Location, 1) too ?
Cheers
You can also try the command too. I found this to work great for me:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Command() <> "" Then
Dim s_Open_File As String = Command.TrimStart(Chr(34)).TrimEnd(Chr(34)) 'Trim the " character in the file name, if " exists
TextBox1.Text = My.Computer.FileSystem.ReadAllText(s_Open_File) 'Load the file into the textbox
End If
End Sub
Yes, that registry setting should correct it.
My.Computer.Registry.SetValue("HKEY_CLASSES_ROOT\Your App Name\DefaultIcon", Nothing, s_App_Location, 1)
You may want to change s_App_Location to s_App_Location & ",1"
Now don't for get to call that sub or function when the form loads or nothing will be set in the registry. So you can just add the MyFileExt() in the Private Sub Form1_Load