 |
|
 |
Hey, thanks a lot for this brilliant article. I just have a couple of questions regarding this to make sure I don't blow up the registry:
1) I have never worked with the registry before, but I do understand what's going on in this coding (without the details of how the registry knows where it has to look for its values, but that isn't that important to me). Would it be okay to associate the file type with my application every time the application is loaded (i.e. in the form load event)? I know that wouldn't be user friendly in case the user wants to open the files with a different program, but even if i do associate the file extension on every startup of my application, can that result in 'double registry entries'? Or will the existing entries be updated/replaced?
2. Of course it would be better to check if the file type is allready associated with my program first and then ask the user if he wants to associate it with my application if this is not the case. Is there a way to do that?
Thanks a lot for any help!!
Ibby
|
|
|
|
 |
|
 |
thanks to do it in VB.Net gave 6*
|
|
|
|
 |
|
 |
Simple, Fast, and Effective!
|
|
|
|
 |
|
|
 |
|
 |
Cool But How do i set the icon for the file that opens?
|
|
|
|
 |
|
 |
I have wrote this "associateFile" function as follow including Icon issue. I hope this can help
Regards
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
associateFile("HELLO", ".hello", "C:\myicon.ico", "My Hello File", Application.ExecutablePath)
End Sub
Public Sub associateFile(ByVal mainKey As String, ByVal ext As String, ByVal iconPath As String, ByVal fileDescription As String, ByVal appPath As String)
My.Computer.Registry.ClassesRoot.CreateSubKey(ext).SetValue("", mainKey, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(mainKey).SetValue("", fileDescription, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(mainKey & "\shell\open\command").SetValue("", appPath & " ""%l"" ", Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(mainKey & "\DefaultIcon").SetValue("", iconPath, Microsoft.Win32.RegistryValueKind.ExpandString)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("Hello, " & My.Computer.FileSystem.ReadAllText(My.Application.CommandLineArgs(0)) & " World!")
End Sub
End Class
|
|
|
|
 |
|
 |
This was a great article! Thank you.
How about an article about command verbs? When you right-click on a file name, the context menu shows Open (usually naming a program) and Print, among other options. An article explaining how these work and how to add new ones should be of interest to those curious about the Windows Registry.
|
|
|
|
 |
|
 |
This is what I've been looking for. I don't mind that it's locked up in a dll. The question is, how do I add the .dll to my project? I went to project and added it as a reference, but it's dimmed out in the Solution Explorer.
I am using VB 2005 on Windows XP.
My app is a text editor. I want to give the users the option of making myapp the default application for .txt and .rtf files. The user will have a button to press to run the sub.
I've tried all the code above in various ways, but always get errors or no results at all.
Please help.
|
|
|
|
 |
|
 |
I don't recommend using the dll as it was full of bugs and not all that well designed, which is why the download for it on its own was removed. Instead follow the instructions in the article and post your implementation here if you have problems.
Edit: I've submitted an update to the article that gets rid of the dll entirely and should act as a much better example.
|
|
|
|
 |
|
 |
This is not creating any errors, but it's also not doing anything at all. The other snippet not included here, is chucked full of errors as soon as I paste it in my project.
Private Sub Association_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Association.Click
Dim rslt As MsgBoxResult
rslt = MsgBox("Are you sure you want .txt and .rtf files to open with this application?", MsgBoxStyle.YesNo, "Change File Association?")
If rslt = MsgBoxResult.Yes Then
AssociateExtFile()
ElseIf rslt = MsgBoxResult.No Then
Exit Sub
End If
End Sub
Private Sub AssociateExtFile()
Try
Dim extension As String = ".txt"
Dim name As String = "textFile"
Dim description As String = "text Formats File"
Dim applicationPath As String = Environment.GetCommandLineArgs()(0)
Dim iconPath As String = Path.Combine(Path.GetDirectoryName(applicationPath), "JWA.ico")
AssociateFile(extension, name, description, iconPath, applicationPath)
Catch ex As Exception
MsgBox("Unable to associate file type" & ex.ToString)
End Try
End Sub
Private Shared Sub AssociateFile(ByVal extension As String, ByVal name As String, ByVal description As String, ByVal iconPath As String, ByVal applicationPath As String)
If String.IsNullOrEmpty(extension) OrElse Not extension.StartsWith(".") Then Throw New ArgumentException("Extension must not be null, must also start with '.'", "extension")
If String.IsNullOrEmpty(description) Then Throw New ArgumentException("description must not be null", "description")
Dim iconFile As FileInfo = Nothing
If iconPath IsNot Nothing Then
iconFile = New FileInfo(iconPath)
If Not iconFile.Exists Then Throw New ArgumentException("Icon file does not exist", "iconPath")
End If
Dim applicationFile As New FileInfo(applicationPath)
If Not applicationFile.Exists Then Throw New ArgumentException("Application file does not exist", "applicationFile")
My.Computer.Registry.ClassesRoot.CreateSubKey(extension).SetValue("", name, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey((name & "\shell\open\command")).SetValue("", (applicationFile.FullName & " ""%l"" "), Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(name).SetValue("", description)
If iconFile IsNot Nothing Then _
My.Computer.Registry.ClassesRoot.CreateSubKey((name & "\DefaultIcon")).SetValue("", iconFile.FullName, Microsoft.Win32.RegistryValueKind.String)
End Sub
|
|
|
|
 |
|
 |
You may need to delete the Progid value located under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt
Does using your code to associate an unused extension work? Does regedit show that everything has been changed correctly?
|
|
|
|
 |
|
 |
not very easy and wat if u want other stuff rather than text
|
|
|
|
 |
|
 |
For any format other than text, you would just read the file however you would normally read that type of file. For example, for an xml file you would just create an instance of a System.Xml.XmlReader.
|
|
|
|
 |
|
 |
Source Code Hidden in DLL!
|
|
|
|
 |
|
 |
Public Shared Sub Associate(ByVal Type As FileType, ByVal ExeFile As String, ByVal Optional ExtraCommands As String = Nothing)
If ((((Type.Description <> Nothing) And (Type.Extension = Nothing)) And (Type.Icon = Nothing)) And (Type.Name = Nothing)) Then
MyProject.Computer.Registry.ClassesRoot.CreateSubKey(Type.Extension).SetValue("", Type.Name, RegistryValueKind.String)
MyProject.Computer.Registry.ClassesRoot.CreateSubKey((Type.Name & "\shell\open\command")).SetValue("", (ExeFile & " ""%l"" " & ExtraCommands), RegistryValueKind.String)
MyProject.Computer.Registry.ClassesRoot.CreateSubKey(Type.Name).SetValue("", Type.Description)
MyProject.Computer.Registry.ClassesRoot.CreateSubKey((Type.Name & "\DefaultIcon")).SetValue("", Type.Icon, RegistryValueKind.String)
Else
Dim innerException As New Exception(String.Concat(New String() { "Type.Name: ", Type.Name, ChrW(13) & ChrW(10) & "Type.Extension: ", Type.Extension, ChrW(13) & ChrW(10) & "Type.Description: ", Type.Description, ChrW(13) & ChrW(10) & "Type.Icon", Type.Icon }))
Throw New Exception("One or more of the FileType's properties are invalid. See innerexception for deatails.", innerException)
End If
End Sub
|
|
|
|
 |
|
 |
And this is the code done right:
Private Shared Sub AssociateFile(ByVal extension As String, ByVal name As String, ByVal description As String, ByVal iconPath As String, ByVal applicationPath As String)
If String.IsNullOrEmpty(extension) OrElse Not extension.StartsWith(".") Then Throw New ArgumentException("Extension must not be null, must also start with '.'", "extension")
If String.IsNullOrEmpty(description) Then Throw New ArgumentException("description must not be null", "description")
Dim iconFile As FileInfo = Nothing
If iconPath IsNot Nothing Then
iconFile = New FileInfo(iconPath)
If Not iconFile.Exists Then Throw New ArgumentException("Icon file does not exist", "iconPath")
End If
Dim applicationFile As New FileInfo(applicationPath)
If Not applicationFile.Exists Then Throw New ArgumentException("Application file does not exist", "applicationFile")
My.Computer.Registry.ClassesRoot.CreateSubKey(extension).SetValue("", name, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey((name & "\shell\open\command")).SetValue("", (applicationFile.FullName & " ""%l"" "), Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(name).SetValue("", description)
If iconFile IsNot Nothing Then _
My.Computer.Registry.ClassesRoot.CreateSubKey((name & "\DefaultIcon")).SetValue("", iconFile.FullName, Microsoft.Win32.RegistryValueKind.String)
End Sub
And this is it being used:
Private Sub AssociateExtFile()
Try
Dim extension As String = ".test"
Dim name As String = "testFile"
Dim description As String = "test Formats File"
Dim applicationPath As String = Environment.GetCommandLineArgs()(0)
Dim iconPath As String = Path.Combine(Path.GetDirectoryName(applicationPath), "test.ico")
AssociateFile(extension, name, description, iconPath, applicationPath)
Catch ex As Exception
MsgBox("Unable to associate file type" & ex.ToString)
End Try
End Sub
|
|
|
|
 |
|
 |
You got my 5
-= Really cool sig goes here =-
|
|
|
|
 |
|
|
 |
|
 |
do you have access to write to the classess rout part of the registry? (try running your app as an administrator first)
Normally any 'install' app needs to be promoted to Admin access.
|
|
|
|
 |
|
 |
Could you provide the source code of the Registry Actions.dll?
Is the source code of your demo project that I just downloaded really up to date?
It does not contain any of the actions from the "screen dump-picture of the form" from your article.
|
|
|
|
 |
|
 |
So what would be used to associate a mailto link with an email program? URL:Mailto doesn't work. What would?
|
|
|
|
 |
|
 |
can you add a function to remove the association safely as well so that the previous program will take over registrations?
Thanks!
|
|
|
|
 |
|
 |
This 100% Works! Its not even hard to follow! very clean, easy, and best of all working
Thumbs up to you!
|
|
|
|
 |
|
 |
Hi, I've tried your dll, with the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fType As New FileType(".fitytest", "filetypetest", "Test of filetypes", "C:\Documents and Settings\Jesper\Skrivebord\free_icons\amc\Standard V.ico")
DoAssociation.Associate(fType, Application.ExecutablePath, """%1""")
End Sub
I've looked in my registry, and it seem to work, but when i open my .fifytest file, windows don't know which program to use. The program aren't shown either.
What have i done wrong?
- galaicra
|
|
|
|
 |
|
 |
New FileType(".fitytest"
Try it without the preceding period.
New FileType("fitytest"
|
|
|
|
 |