How can my application reliably set up, pre-publishing, an *effective* association between a file extension and itself, programmatically or otherwise?

  • Thread starter Thread starter RobertGustafson
  • Start date Start date
R

RobertGustafson

Guest
WHAT I HAVE:

Visual Basic 2019, .NET 4.6.1, WinForms

MY PROBLEM:

I need a fool-proof way for my program to determine if files with a certain kind of extension are associated with the program, and if not, then to associate accordingly, during program execution. I know how to set up an association for the "published" version, but I have no way to effectively set it up before publishing it. When I try the following, I get an UnauthorizedAccessException, at least when debugging from the Visual Studio environment:

Const SocialContactListExtension As String = "sct"
With My.Computer.Registry.ClassesRoot
.CreateSubKey("." & SocialContactListExtension).SetValue("", _
SocialContactListExtension, Microsoft.Win32.RegistryValueKind.String)
.CreateSubKey(SocialContactListExtension & "\shell\open\command").SetValue("", _
Application.ExecutablePath & " ""%1"" ", _
Microsoft.Win32.RegistryValueKind.String)
End With

I also know that if the "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." & SocialContactListExtension key has a value, then the value has to be deleted (in order for the above code to affect the association) or modified (in order for a new association otherwise to be set). I'm not sure how to look for it and properly change it programmatically. Also, if I manually delete the HKEY_CLASSES_ROOT key ".sct" and the previously mentioned HKEY_CURRENT_USER subkey entry ".sct" using the Registry Editor (dangerous!), I still get the UnauthorizedAccessException.

I need a fool-proof way (using VB) for an association to be created between my application and an extension at run-time and (in any case) before publishing. I know I could wait until I publish it, but that would deny me the ability to test the association (from outside the environment, in Windows Explorer).


PS. If there's a way to set up the association and make it take effect from VB2019 pre-publishing using the IDE (as opposed to programmatically), I'd like to know that too. Current documentation suggests that one has to manually create the association using XML--which is tedious (!!)--when in fact one can set it up in the IDE using Project > Properties > Publish > Options > File Associations. (Why this feature isn't properly documented, I don't know.) The only thing is, the association doesn't take effect until the app is published.



Robert Gustafson

Continue reading...
 
Back
Top