Ho tribolato abbastanza per trovare una soluzione decorosa al problema in oggetto e alla fine ne ho scritta una io che …
… non ha la pretesa di essere una roba professionale, che funziona sempre ed ovunque ma almeno può rappresentare qualcosa di usabile.
Il problema: ho un’estensione, diciamo “.pdf” e devo trovare il percorso completo del programma al quale – presumibilmente – è associata.
Questo che propongo di seguito è il codice in VB.NET:
Public Shared Function GetAssociatedProgram(sFileExtension As String) As String
If sFileExtension.Substring(0, 1) <> "." Then sFileExtension = "." + sFileExtension
Try
Dim sContentType As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\" + sFileExtension, "Content Type", Nothing)
If sContentType IsNot Nothing Then
Dim sAppId As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\MIME\Database\Content Type\" + sContentType, "", Nothing)
If sAppId IsNot Nothing Then
Dim sAppCommand As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\" + sAppId + "\shell\open\command", "", Nothing)
If sAppCommand IsNot Nothing Then
Return sAppCommand.Split("%1")(0).Trim()
End If
End If
End If
Catch ex As Exception
Debug.Print(ex.ToString)
End Try
Return ""
End Function
Il problema in oggetto l’ho risolto anche con dei comandi batch (sì, mi serviva la cosa in un file “.bat”) ma, se proprio qualcuno è interessato (scriva pure un commento sotto), vedrò di pubblicarlo in articolo dedicato.
Se trovate bug o avete suggerimenti, li accetto molto volentieri !