Adding file associations to VB.NET OneClick Applications
I struggled to find out how to do all of this. There are lots of suggestions on the net, but none that I found worked properly. You can't (or at least I couldn't figure out how to) add file associations through Explorer->Tools->Folder Options as the exe file doesn't launch like a normal windows app. To start with, it is hidden in "C:\Documents and Settings\UserName\Local Settings\Apps\2.0\....\...\..." (Replace the dots with random characters and guids!). Also, if I upgrade, the folder changes. When I tracked down the right exe and associated a file type with it I was getting reports that the file I double clicked isn't a valid executable. I knew that - I didn't want to execute it - I wanted to open it in my new editor!I found plenty of articles on how to manually (programatically) edit the registry but all these had the same result as using Folder Options.
Anyway, I eventually located enough information to get this working the way I think it was intended, so I decided to blog it here.
I hope you find this helpful.
Add the icon file
I'm using Visual Studio 10 Express edition. This doesn't have an icon editor but I have an old copy of VC6 Pro that does, so I use this. I couldn't find a free editor but there are various free bmp to ico converters on the web, plus a ton of free icon files.Add the icon to your project (Project->Add Existing Item).
Adding the file associations
Go to Project Properties->Publish->Options->File AssociationsEnter the file extension, the description (which will come up in explorer), a unique identifier (e.g. a guid), and select the icon for this file type.
This is good as far as it goes. If you publish/install the app it should set up the registry entries correctly, you will get the icon on your files and double clicking will open your program.
Opening the file
Having got this far I couldn't find out how you discover which file had been double clicked to launch the program. This was partly because I had migrated from VB6. I was trying to use the VB.Command() which retrieves the command line that launched the program, but this was always empty when the application had been installed. It worked fine with debug command line options. I found the correct way to find the file from a Click Once installation was to examine AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData, but you have to check it isn't empty first or ActivationData.Count will throw an exception.Here is my form load code:
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim FileName As String
FileName = ""
If Not String.IsNullOrEmpty(VB.Command()) Then
'search for any extra command line parameters
FileName = VB.Command()
Else
If Not IsNothing(AppDomain.CurrentDomain.SetupInformation.ActivationArguments) Then
If Not IsNothing(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData) Then
If Not IsNothing(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Count) Then
For i As Integer = 0 To AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Count - 1
'search for any extra command line parameters
FileName = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData(i)
Next i
End If
End If
End If
End If
Me.Show()
initalise() ' initialise system globals
'get command line file name and open it if present
If (FileName <> "") Then
LoadFile(FileName)
Else
Me.FileNew()
End If
End Sub
n.b. When using ClickOnce you will find it sets the ClickOnce Security Settings every time you publish your program. Then, the next time you try to debug with any command line options (Project Properties->Debug->Command line arguments) it warns you that "command line arguments will not be passed to the executable". You can turn this off by clicking Project Properties->Security; Select partial trust; click on Advanced and turn off "Debug this application with the selected permissions set"; OK; Finally, select full trust.