<system.runtime.interopservices.dllimport("shell32.dll")> Shared Sub
SHChangeNotify(ByVal wEventId As Integer, ByVal uFlags As Integer,
ByVal dwItem1 As Integer, ByVal dwItem2 As Integer)
End Sub
' Create the new file association
'
' Extension is the extension to be registered (eg ".cad"
' ClassName is the name of the associated class (eg "CADDoc")
' Description is the textual description (eg "CAD Document"
' ExeProgram is the app that manages that extension (eg "c:\Cad\MyCad.exe") </system.runtime.interopservices.dllimport("shell32.dll")>
Function CreateFileAssociation(ByVal extension As String,
ByVal className As String, ByVal description As String,
ByVal exeProgram As String) As Boolean
Const SHCNE_ASSOCCHANGED As Integer = &H8000000
Const SHCNF_IDLIST As Integer = 0
' ensure that there is a leading dot
If extension.Substring(0, 1) <> "." Then
extension = "." & extension
End If
Dim key1 As Microsoft.Win32.RegistryKey = Nothing
Dim key2 As Microsoft.Win32.RegistryKey = Nothing
Dim key3 As Microsoft.Win32.RegistryKey = Nothing
Try
' create a value for this key that contains the classname
key1 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension)
key1.SetValue("", className)
' create a new key for the Class name
key2 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(className)
key2.SetValue("", description)
' associate the program to open the files with this extension
key3 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(className & _
"\Shell\Open\Command")
key3.SetValue("", exeProgram & " ""%1""")
Catch e As Exception
Return False
Finally
If Not key1 Is Nothing Then key1.Close()
If Not key2 Is Nothing Then key2.Close()
If Not key3 Is Nothing Then key3.Close()
End Try
' notify Windows that file associations have changed
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0)
Return True
End Function
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
Is there a way to make my player associated with media files?
I would like avi files and mpg files to be associated with the player.
Thanks
In visual basic you can do it like this:
<system.runtime.interopservices.dllimport("shell32.dll")> Shared Sub
SHChangeNotify(ByVal wEventId As Integer, ByVal uFlags As Integer,
ByVal dwItem1 As Integer, ByVal dwItem2 As Integer)
End Sub
' Create the new file association
'
' Extension is the extension to be registered (eg ".cad"
' ClassName is the name of the associated class (eg "CADDoc")
' Description is the textual description (eg "CAD Document"
' ExeProgram is the app that manages that extension (eg "c:\Cad\MyCad.exe") </system.runtime.interopservices.dllimport("shell32.dll")>
Function CreateFileAssociation(ByVal extension As String,
ByVal className As String, ByVal description As String,
ByVal exeProgram As String) As Boolean
Const SHCNE_ASSOCCHANGED As Integer = &H8000000
Const SHCNF_IDLIST As Integer = 0
' ensure that there is a leading dot
If extension.Substring(0, 1) <> "." Then
extension = "." & extension
End If
Dim key1 As Microsoft.Win32.RegistryKey = Nothing
Dim key2 As Microsoft.Win32.RegistryKey = Nothing
Dim key3 As Microsoft.Win32.RegistryKey = Nothing
Try
' create a value for this key that contains the classname
key1 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension)
key1.SetValue("", className)
' create a new key for the Class name
key2 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(className)
key2.SetValue("", description)
' associate the program to open the files with this extension
key3 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(className & _
"\Shell\Open\Command")
key3.SetValue("", exeProgram & " ""%1""")
Catch e As Exception
Return False
Finally
If Not key1 Is Nothing Then key1.Close()
If Not key2 Is Nothing Then key2.Close()
If Not key3 Is Nothing Then key3.Close()
End Try
' notify Windows that file associations have changed
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0)
Return True
End Function
Thanks, any C# examples?
Use a free converter. One of many can be found here:
http://www.developerfusion.com/tools/convert/csharp-to-
vb/
http://www.developerfusion.com/tools/convert/csharp-to-vb/](http://text)
It doesnt work, when I try to open an AVI file it uses wmp. When I right click
the file and go to open with, it shows my software nameless.
I want it to open the file with my software and not add it to the open with
list.
Is there a way to do this?
Se the link below. It's in vb but it shouldn't be too difficult.
http://www.codeproject.com/KB/vb/VBFileAssociation.aspx