none
Right Click File Folder Question RRS feed

  • Question

  • I'm trying to develop a program that is executed when I "right click" a folder or file in the explorer. Also would like to have the path of the file or folder clicked on.

    I know somehow I must add an item to the "Explorter Menu" but don't know how. Then I need the file or directory name that was richt clicked on passed to my program from the explorer, not sure how?

    Best Regards
    Monday, April 20, 2009 6:34 PM

Answers

  • Hello all,

    Actually, there are two ways to achieve this.

    1. Create a shell extension application. Please see http://www.theserverside.net/tt/articles/showarticle.tss?id=ShellExtensions

    But Microsoft does not recommend go this way using managed codes and does not support this approach. See the following blog articles to learn why,
    http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx

    So if you want to create a shell extension application, you'd better go through the unmanaged C++.
    http://msdn.microsoft.com/en-us/magazine/cc188741.aspx
    http://msdn.microsoft.com/en-us/library/cc144067(VS.85).aspx

    2. Use the registry to control the shell context menu as kaymaf already showed. But I want to add some points. His codes only add keys in the HKCR\Folder\Shell. So the item will only appear when we right click a folder, not a file. As per your demand, I think we need to add keys into the HKCR\AllFilesystemObjects\Shell.

    So the following is detailed steps,

    1. Open your regedit
    2. Navigate to HKCR\AllFilesystemObjects
    3. Create a new Key named shell if it does not exist.
    4. Under the shell, create a new key whose name will appear as the menu item.
    5. Under that key created in step4, create a new key named command
    6. Set created command key's default value to {yourapplicationfullpath -d "%1"}

    For example, I use notepad in my side to have a test, so I will have the following key,
    HKCR\AllFilesystemObjects\shell\Open in notepad\command (its default value is notepad -d "%1" )

    Then when I right click a file in my explorer, I can find the option Open in notepad. If I click that option and the selected file is .txt, it will be opened in notepad.

    In you application, to recieve the parameter, you just need to write a parameterized Main method or call the System.Environment.GetCommandLineArgs()


    Best regards,
    Colbert
    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    • Proposed as answer by Ji.ZhouModerator Tuesday, April 21, 2009 1:35 PM
    • Marked as answer by VBDotNetMan Tuesday, April 21, 2009 2:31 PM
    Tuesday, April 21, 2009 12:44 PM
    Moderator
  • You can try this for folder, i used this with my Change folder icon, when i right click on any folder, it will send the folder name to my program which is kayFolderIcon. I have two methods add and remove. Anywhere you see kayFolderIcon, replace it with your program name that you want to send the folder name. You can change contextname value to whatever you want
    #Region "contextMENU"
    Imports Microsoft.Win32
         Private Const MenuName As String = "Folder\shell\kayFolderIcon" 'kayFolderIcon is the name of my program
        Private Const Command As String = "Folder\shell\kayFolderIcon\command"
        Dim contextname As String = "Change Folder Icon with kayFolderIcon" 'this will be display in windows explorer context menu when you right click a folder,you 
        Dim applicationName As String = Application.ExecutablePath & " ""%1"""  'this will point to my program file
        Function AddContext()
            Dim regmenu As RegistryKey = Nothing
            Dim regcmd As RegistryKey = Nothing
    
            Try
                regmenu = Registry.ClassesRoot.CreateSubKey(MenuName)
                If Not regmenu Is Nothing Then
                    regmenu.SetValue("", contextname)
                End If
                regcmd = Registry.ClassesRoot.CreateSubKey(Command)
                If Not regcmd Is Nothing Then
                    regcmd.SetValue("", applicationName)
                End If
    
            Catch ex As Exception
                MsgBox(ex.Message.ToString())
            Finally
                If Not regmenu Is Nothing Then
                    regmenu.Close()
                End If
                If Not regcmd Is Nothing Then
                    regcmd.Close()
                End If
            End Try
        Return Nothing
        End Function
    
        Function RemoveContext()
            Try
                Dim reg As RegistryKey = Registry.ClassesRoot.OpenSubKey(Command)
                If Not reg Is Nothing Then
                    reg.Close()
                    Registry.ClassesRoot.DeleteSubKey(Command)
                End If
                reg = Registry.ClassesRoot.OpenSubKey(MenuName)
                If Not reg Is Nothing Then
                    reg.Close()
                    Registry.ClassesRoot.DeleteSubKey(MenuName)
                End If
            Catch ex As Exception
                MsgBox(ex.Message.ToString())
            End Try
    Return Nothing
        End Function
    
    #End Region
    To get the folder name in your application, put readApplicationCommand() in formLoad event, i never test it on multiple folders.
    Function readApplicationCommand()
            For Each param As String In Environment.GetCommandLineArgs
                Try
    
                    OpenFromPath(param)
    
                Catch
    
                End Try
    
            Next param
        End Function
    
    Public Sub OpenFromPath(ByVal sFilePath As String) As String
            Application.DoEvents()
           Dim pth as string=Nothing
            If Directory.Exists(sFilePath) Then
              TextBox1.Text = sFilePath
             pth=sFilePath
             Return pth
             End If
          Return pth
        End Sub
    

    kaymaf

    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Proposed as answer by Ji.ZhouModerator Tuesday, April 21, 2009 1:35 PM
    • Marked as answer by VBDotNetMan Tuesday, April 21, 2009 2:32 PM
    Monday, April 20, 2009 7:30 PM

All replies

  • You can try this for folder, i used this with my Change folder icon, when i right click on any folder, it will send the folder name to my program which is kayFolderIcon. I have two methods add and remove. Anywhere you see kayFolderIcon, replace it with your program name that you want to send the folder name. You can change contextname value to whatever you want
    #Region "contextMENU"
    Imports Microsoft.Win32
         Private Const MenuName As String = "Folder\shell\kayFolderIcon" 'kayFolderIcon is the name of my program
        Private Const Command As String = "Folder\shell\kayFolderIcon\command"
        Dim contextname As String = "Change Folder Icon with kayFolderIcon" 'this will be display in windows explorer context menu when you right click a folder,you 
        Dim applicationName As String = Application.ExecutablePath & " ""%1"""  'this will point to my program file
        Function AddContext()
            Dim regmenu As RegistryKey = Nothing
            Dim regcmd As RegistryKey = Nothing
    
            Try
                regmenu = Registry.ClassesRoot.CreateSubKey(MenuName)
                If Not regmenu Is Nothing Then
                    regmenu.SetValue("", contextname)
                End If
                regcmd = Registry.ClassesRoot.CreateSubKey(Command)
                If Not regcmd Is Nothing Then
                    regcmd.SetValue("", applicationName)
                End If
    
            Catch ex As Exception
                MsgBox(ex.Message.ToString())
            Finally
                If Not regmenu Is Nothing Then
                    regmenu.Close()
                End If
                If Not regcmd Is Nothing Then
                    regcmd.Close()
                End If
            End Try
        Return Nothing
        End Function
    
        Function RemoveContext()
            Try
                Dim reg As RegistryKey = Registry.ClassesRoot.OpenSubKey(Command)
                If Not reg Is Nothing Then
                    reg.Close()
                    Registry.ClassesRoot.DeleteSubKey(Command)
                End If
                reg = Registry.ClassesRoot.OpenSubKey(MenuName)
                If Not reg Is Nothing Then
                    reg.Close()
                    Registry.ClassesRoot.DeleteSubKey(MenuName)
                End If
            Catch ex As Exception
                MsgBox(ex.Message.ToString())
            End Try
    Return Nothing
        End Function
    
    #End Region
    To get the folder name in your application, put readApplicationCommand() in formLoad event, i never test it on multiple folders.
    Function readApplicationCommand()
            For Each param As String In Environment.GetCommandLineArgs
                Try
    
                    OpenFromPath(param)
    
                Catch
    
                End Try
    
            Next param
        End Function
    
    Public Sub OpenFromPath(ByVal sFilePath As String) As String
            Application.DoEvents()
           Dim pth as string=Nothing
            If Directory.Exists(sFilePath) Then
              TextBox1.Text = sFilePath
             pth=sFilePath
             Return pth
             End If
          Return pth
        End Sub
    

    kaymaf

    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Proposed as answer by Ji.ZhouModerator Tuesday, April 21, 2009 1:35 PM
    • Marked as answer by VBDotNetMan Tuesday, April 21, 2009 2:32 PM
    Monday, April 20, 2009 7:30 PM
  • Hi Kaymaf,

    Thank you for your reply and code. I'm not sure this is what I need?

    I need to add a menu item to the "Explorer Menu" when someone right clicks on a folder or file. I then need to wire that menu item to my program so that when the menu item is clicked my program is executed passing my program the file or folder name.



    Question is: How do I modify the windows xp "Explorer Menu" that appears when a folder or file is right clicked. I'd like to add an "menu item" to it"
                      How do I get the file or folder name when I click my "menu item"?


    Best Regards
    Monday, April 20, 2009 7:58 PM
  • why dont you read it and test it, you want your application to show in windows explorer when someone right click a folder that is what i did. You cannot do that within your application, you have to write it to registry and that is code i used.

    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Monday, April 20, 2009 10:14 PM
  • It worked great, Thank You so much. I could not find any documentation on the MSDN Library for this and I didn't think it would be this easy. Some of the blogs I read said that I would have to use "Shell Extensions".

    Was there one place where you found the documentation for this?

    Best Regards
    Tuesday, April 21, 2009 11:32 AM
  • Hello all,

    Actually, there are two ways to achieve this.

    1. Create a shell extension application. Please see http://www.theserverside.net/tt/articles/showarticle.tss?id=ShellExtensions

    But Microsoft does not recommend go this way using managed codes and does not support this approach. See the following blog articles to learn why,
    http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx

    So if you want to create a shell extension application, you'd better go through the unmanaged C++.
    http://msdn.microsoft.com/en-us/magazine/cc188741.aspx
    http://msdn.microsoft.com/en-us/library/cc144067(VS.85).aspx

    2. Use the registry to control the shell context menu as kaymaf already showed. But I want to add some points. His codes only add keys in the HKCR\Folder\Shell. So the item will only appear when we right click a folder, not a file. As per your demand, I think we need to add keys into the HKCR\AllFilesystemObjects\Shell.

    So the following is detailed steps,

    1. Open your regedit
    2. Navigate to HKCR\AllFilesystemObjects
    3. Create a new Key named shell if it does not exist.
    4. Under the shell, create a new key whose name will appear as the menu item.
    5. Under that key created in step4, create a new key named command
    6. Set created command key's default value to {yourapplicationfullpath -d "%1"}

    For example, I use notepad in my side to have a test, so I will have the following key,
    HKCR\AllFilesystemObjects\shell\Open in notepad\command (its default value is notepad -d "%1" )

    Then when I right click a file in my explorer, I can find the option Open in notepad. If I click that option and the selected file is .txt, it will be opened in notepad.

    In you application, to recieve the parameter, you just need to write a parameterized Main method or call the System.Environment.GetCommandLineArgs()


    Best regards,
    Colbert
    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    • Proposed as answer by Ji.ZhouModerator Tuesday, April 21, 2009 1:35 PM
    • Marked as answer by VBDotNetMan Tuesday, April 21, 2009 2:31 PM
    Tuesday, April 21, 2009 12:44 PM
    Moderator
  • Hi Colbert,

    Thanks so much for your reply. I noticed this morning the Menu Item did not appear when I right clicked on a file.

    Is there some place on the MSDN library where I can read in detail on the process you outlined in step 2?


    Best Regards
    Tuesday, April 21, 2009 1:07 PM
  • I searched but did not find related official documents on this. What about the following two nice articles from MVP site and codeproject,

    http://windowsxp.mvps.org/context_folders.htm
    http://www.codeproject.com/KB/shell/SimpleContextMenu.aspx

    The first one not only summarizes all kinds of objects that we can customize, but also includes a tool.
    The second one is a complete sample project.


    Regards, Colbert
    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    Tuesday, April 21, 2009 1:18 PM
    Moderator
  • yeah, i said earlier that the code i posted only work for folder, if you walk through colbert steps above you can modified my code to use it for files
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Tuesday, April 21, 2009 1:35 PM
  • Thank you both for your help, Thanks to both replies I have enough to continue on my project.

    Many Thanks........
    Tuesday, April 21, 2009 2:33 PM
  • Hello all,

    Actually, there are two ways to achieve this.

    1. Create a shell extension application. Please see http://www.theserverside.net/tt/articles/showarticle.tss?id=ShellExtensions

    But Microsoft does not recommend go this way using managed codes and does not support this approach. See the following blog articles to learn why,
    http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx

    So if you want to create a shell extension application, you'd better go through the unmanaged C++.
    http://msdn.microsoft.com/en-us/magazine/cc188741.aspx
    http://msdn.microsoft.com/en-us/library/cc144067(VS.85).aspx

    2. Use the registry to control the shell context menu as kaymaf already showed. But I want to add some points. His codes only add keys in the HKCR\Folder\Shell. So the item will only appear when we right click a folder, not a file. As per your demand, I think we need to add keys into the HKCR\AllFilesystemObjects\Shell.

    So the following is detailed steps,

    1. Open your regedit
    2. Navigate to HKCR\AllFilesystemObjects
    3. Create a new Key named shell if it does not exist.
    4. Under the shell, create a new key whose name will appear as the menu item.
    5. Under that key created in step4, create a new key named command
    6. Set created command key's default value to {yourapplicationfullpath -d "%1"}

    For example, I use notepad in my side to have a test, so I will have the following key,
    HKCR\AllFilesystemObjects\shell\Open in notepad\command (its default value is notepad -d "%1" )

    Then when I right click a file in my explorer, I can find the option Open in notepad. If I click that option and the selected file is .txt, it will be opened in notepad.

    In you application, to recieve the parameter, you just need to write a parameterized Main method or call the System.Environment.GetCommandLineArgs()


    Best regards,
    Colbert
    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    Your second method is just like what I did with my Attributor 2.0 app and it worked so, but so I am writing a windows forms application i n VB do I just add that...
    dim parameters as system.environment.getcommandlineargs()
    textbox1.text = parameters
    I haven't tried it out but and that would go into window load section...? eather way thanks for the help. :)
    Tuesday, June 16, 2009 2:22 AM
  • I dont know if that will work because system.environment.getcommandlineargs() is an array of string. so if that doesnt work then try with for each loop, you will still get the same. Yeah, put it in Form_Load event

    For Each param As String In Environment.GetCommandLineArgs
                Try
    
                   Listbox1.Items.Add(param)
    
                Catch
    
                End Try
    
            Next param
    ' the above code will works if someone right click more than 1 file or folder.
    'If you want just 1 file or folder at a time, then try this
    
    Dim AllParam() as String= Environment.GetCommandLineArgs()
    textbox1.text=AllParam(0) ' this will give you the first item on the list
    
    
    
    kaymaf

    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Tuesday, June 16, 2009 11:52 AM
  • Application.CommandLineArgs will get you the command line arguments
    Tuesday, June 16, 2009 9:27 PM
  • %1 will give me the path of one file. It works fine to this point.
    However, in my case, the user can select any number of files and do a right to select  my custom menu item, in which case my exe associated with context menu should be able to pick the path of all the files selected. How can I do that ???

    Thanks
    Saturday, October 24, 2009 2:14 AM
  • Thanks it working.

    Wednesday, October 3, 2012 11:58 AM