none
How to show different file extensions for same application in explorer. RRS feed

  • Question

  • I have several file types that open with the same program,  .qsc, .qsb, .qsj, .qsb5, .qsc5.

    They are 

    .qsb  Quick Steel Beam file

    .qsc  Quick Steel Column file

    .qsj  Quick Steel Joist file

    .qsb5 and .qsc5 are updated versions of the qsb and qsc files.

    I used the following to create the file assocations: 

     Public Sub CreateFileAssociation(AppName As String, AppDesc As String, ExtnName As String)
    
            With My.Computer.Registry.ClassesRoot
    
                'Create the extension key linked to the AppName key
                .CreateSubKey(ExtnName).SetValue("", AppName, Microsoft.Win32.RegistryValueKind.String)
    
                'Create the file description that is displayed in Windows Explorer under the AppName key's default value
                .CreateSubKey(AppName).SetValue("", AppDesc.Trim, Microsoft.Win32.RegistryValueKind.String)
    
                'Create the default icon key under the AppName key
                .CreateSubKey(AppName & "\DefaultIcon").SetValue _
                    ("", Application.ExecutablePath & ",0", Microsoft.Win32.RegistryValueKind.String)
    
                'Create the link to run the program under the AppName key
                .CreateSubKey(AppName & "\shell\open\command").SetValue _
                    ("", Application.ExecutablePath & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
            End With
    
        End Sub

    I use the following to call the routine for the various file types:

            CreateFileAssociation(Application.ProductName, "Quick Steel Beam DataFile ", ".qsb5")
            CreateFileAssociation(Application.ProductName, "Quick Steel Column DataFile", ".qsc5")
            CreateFileAssociation(Application.ProductName, "Quick Steel Joist DataFile ", ".qsj")

    But when I run the program, all the various types show the same, "Quick Steel Joist DataFile", since that was the last call.

    Is there a way to have the different file type descriptions for each file extension but still be able to open them with the same application?

    Thursday, May 4, 2017 3:45 PM

Answers

  • Aha.

    The trick is to make sure the value of the subkey is unique, so that it creates a different heading in the registry that reads the description that matches the extension. 

    So I modified my routine above to thus:

     Public Sub CreateFileAssociation(AppName As String, ExtnIndex As Integer, AppDesc As String, ExtnName As String)
    
            With My.Computer.Registry.ClassesRoot
    
                'Creates a unique heading in the registry for each file description (AppDesc) category
                AppName = AppName & ".K" & ExtnIndex.ToString
    
                'Create the extension key linked to the AppName key
                .CreateSubKey(ExtnName).SetValue("", AppName, Microsoft.Win32.RegistryValueKind.String)
    
                'Create the file description that is displayed in Windows Explorer under the AppName key's default value
                .CreateSubKey(AppName).SetValue("", AppDesc.Trim, Microsoft.Win32.RegistryValueKind.String)
    
                'Create the default icon key under the AppName key
                .CreateSubKey(AppName & "\DefaultIcon").SetValue _
                    ("", Application.ExecutablePath & ",0", Microsoft.Win32.RegistryValueKind.String)
    
                'Create the link to run the program under the AppName key
                .CreateSubKey(AppName & "\shell\open\command").SetValue _
                    ("", Application.ExecutablePath & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
            End With
    
        End Sub

    And the calling code:

            CreateFileAssociation(Application.ProductName, 0, "Quick Steel Beam Data File  ", ".qsb5")
            CreateFileAssociation(Application.ProductName, 1, "Quick Steel Column Data File", ".qsc5")
            CreateFileAssociation(Application.ProductName, 1, "Quick Steel Column Data File", ".qsc")
            CreateFileAssociation(Application.ProductName, 2, "Quick Steel Joist Data File ", ".qsj")
            CreateFileAssociation(Application.ProductName, 0, "Quick Steel Beam Data File  ", ".qsb")

    Which sets up the registry keys thus (using  Application.ProductName = "QuickBeam 5.0"

    QuickBeam 5.0.K0 -> Quick Steel Beam Data File

    QuickBeam 5.0.K1 -> Quick Steel Column Data File

    QuickBeam 5.0.K2 -> Quick Steel Joist Data File


    Friday, May 5, 2017 6:27 PM

All replies

  • I really don't think so.  The name is associated with the app that will open the file not the file extension.  For example on my PC I use VLC to play videos.  I have quite a selection of avi/mkv/mp4 all of which have VLC as the "Type".


    Lloyd Sheen

    Thursday, May 4, 2017 4:46 PM
  • Hi Allen,

    I think it is ok.

    I find the sample that you can refer to.

    https://www.codeproject.com/Articles/18594/File-Association-in-VB-NET

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 5, 2017 9:16 AM
    Moderator
  • Your code only sets the default icon.  Take a look at the CodeProject link provided by Cherry and note how they make registry entries for the specific file extension.

    Instead of manually doing this in code, you could publish your application as a Click-Once deployment and just specify the file associations in the click once options dialog.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, May 5, 2017 4:31 PM
    Moderator
  • I don't see in Cherry's link where it sets up anything for different extensions.

    The method in that app is pretty much what I posted above.

    I tried the Click-Once Settings but it didn't change anything (and yes, I commented my CreateAssociation code block out).

    (The reason why is that the ProgID values for all the extensions contained the same text "DWA-QuickBeam 5.0".  It should different for each item, see the Answer below)


    Friday, May 5, 2017 4:52 PM
  • I don't see in Cherry's link where it sets up anything for different extensions.

    The method in that app is pretty much what I posted above.

    I will try your Click-Once settings though.

    I would go the Click-Once route, but for completeness, the relevant section of that article is:

    My.Computer.Registry.ClassesRoot.CreateSubKey(".hello").SetValue_
    	("", "Hello", Microsoft.Win32.RegistryValueKind.String)
    My.Computer.Registry.ClassesRoot.CreateSubKey_
    	("Hello\shell\open\command").SetValue("", Application.ExecutablePath & _
    	" ""%l"" ", Microsoft.Win32.RegistryValueKind.String)

    What does all this do? If you don't understand My.Computer.Registry, here's a link to it on MSDN, or look below:

    Code What it does
    CreateSubKey(".hello") Creates a registry key in ClassesRoot for the .hello extension. Note that you must include the beginning period.
    .SetValue("", "Hello"...
    1. "" (Or Nothing) sets the default value of the key.
    2. "Hello" is like the "txtfile" we saw earlier, it tells which registry key contains the information about the .hello extension.
    CreateSubKey("Hello" & _ "\shell\open\command") This creates the "Hello" sub-key and the "store\open\command" subkey that is needed to store the path to the application that will open this file type.
    .SetValue("", Application.ExecutablePath & _ " ""%l"" ",...
    1. Again, "" tells the application to set the key's default value.
    2. Application.ExecutablePath tells the code to associate the currently running executable with this file type.
    3. " ""%1"" " passes the opened file's location to your program. The quotes around it are optional, but if you have more than one argument, you must put them around each.

    Now run your application once. It will edit the registry. Your program is now associated with the .hello file!


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 5, 2017 4:59 PM
    Moderator
  • See above.
    Friday, May 5, 2017 5:04 PM
  • See above.

    Oh, the edit...

    Did you delete your registry keys first?  Maybe that's causing a problem.  This should work as advertised.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 5, 2017 6:23 PM
    Moderator
  • Aha.

    The trick is to make sure the value of the subkey is unique, so that it creates a different heading in the registry that reads the description that matches the extension. 

    So I modified my routine above to thus:

     Public Sub CreateFileAssociation(AppName As String, ExtnIndex As Integer, AppDesc As String, ExtnName As String)
    
            With My.Computer.Registry.ClassesRoot
    
                'Creates a unique heading in the registry for each file description (AppDesc) category
                AppName = AppName & ".K" & ExtnIndex.ToString
    
                'Create the extension key linked to the AppName key
                .CreateSubKey(ExtnName).SetValue("", AppName, Microsoft.Win32.RegistryValueKind.String)
    
                'Create the file description that is displayed in Windows Explorer under the AppName key's default value
                .CreateSubKey(AppName).SetValue("", AppDesc.Trim, Microsoft.Win32.RegistryValueKind.String)
    
                'Create the default icon key under the AppName key
                .CreateSubKey(AppName & "\DefaultIcon").SetValue _
                    ("", Application.ExecutablePath & ",0", Microsoft.Win32.RegistryValueKind.String)
    
                'Create the link to run the program under the AppName key
                .CreateSubKey(AppName & "\shell\open\command").SetValue _
                    ("", Application.ExecutablePath & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
            End With
    
        End Sub

    And the calling code:

            CreateFileAssociation(Application.ProductName, 0, "Quick Steel Beam Data File  ", ".qsb5")
            CreateFileAssociation(Application.ProductName, 1, "Quick Steel Column Data File", ".qsc5")
            CreateFileAssociation(Application.ProductName, 1, "Quick Steel Column Data File", ".qsc")
            CreateFileAssociation(Application.ProductName, 2, "Quick Steel Joist Data File ", ".qsj")
            CreateFileAssociation(Application.ProductName, 0, "Quick Steel Beam Data File  ", ".qsb")

    Which sets up the registry keys thus (using  Application.ProductName = "QuickBeam 5.0"

    QuickBeam 5.0.K0 -> Quick Steel Beam Data File

    QuickBeam 5.0.K1 -> Quick Steel Column Data File

    QuickBeam 5.0.K2 -> Quick Steel Joist Data File


    Friday, May 5, 2017 6:27 PM