none
how does a vb.net program recognize a parameter passed from windows explorer? RRS feed

  • Question

  • I modified my registry (with regedit) so that  there is a command  executed when the  user  right clicks on a folder  and then chooses the word "describe".  

    The registry key that I put in is something like

    "c:\.......long path.....\myprogram.exe %1"

    I'm hoping %1 passes the name of the folder.

    So in vb.net, I have  a module  with sub main, and I want  to capture  that  argument.

    I'm using:

      Dim returnValue As String()
            returnValue = Environment.GetCommandLineArgs()
            If returnValue.Length > 1 Then
                  GlobalVariables.folder = returnValue(1)
            End  If

    But it  does not work.   What am I doing wrong?

    Sunday, December 3, 2017 8:09 PM

Answers

  •  Looking at the value you set to the Command key in the registry,  if that is how you wrote it,  then it is not right.  The path and filename should have quotes around it,  and the %1 should have another set of quotes around it too.  There should be 4 quotes in all.  Here is an example of what it should look like...  note there is a single space between the quote at the end of the path and the quote before the %1.

    "c:\.......long path.....\myprogram.exe" "%1"

     You should also make sure you have set that path in the Default value of the Command key you created under your describe key.  Also make sure it is under the correct registry key for what you want.  You can look at This Link to see that if it is a Folder you are right clicking on,  then you would create your keys under the registrykey below.

    HKEY_CLASSES_ROOT\Directory\shell

     

     For example,  i created an item in the Folder right click menu called "My Command" and it opens the "MyApplication.exe" file and passes the folder path to it in the first argument (%1).

     

     Here is the code i used in the "MyApplication.exe" application i created for this example.  It has 1 label added to it to display the folder path.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim args() As String = Environment.GetCommandLineArgs
            If args.Length > 1 Then
                Label1.Text = "You clicked (My Command) on this folder:" & vbNewLine & args(1)
            End If
        End Sub
    
    End Class
     

     And here it shows the example in use.  I right click on the "Animated Circle Cursor" folder and select the "My Command" item i added to the system folder menu.  That opens my application and displays the folder path from the args.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 4, 2017 3:03 AM
    • Marked as answer by Gidmaestro Monday, December 4, 2017 10:46 AM
    Monday, December 4, 2017 3:01 AM
  • GetCommandLineArgs is an array, element 0 is the program with it's path, element 1 is populated when a argument is passed in e.g.

    Test it like this

    Module Module1
    
        Sub Main()
            Environment.GetCommandLineArgs() _
                .ToList _
                .ForEach(Sub(arg) Console.WriteLine(arg))
    
            Console.ReadLine()
        End Sub
    
    End Module
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, December 4, 2017 2:10 AM
    Moderator

All replies

  • Hi Gidmaestro,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about VB.NET development. Since our forum is to discuss the VS IDE and I will help you move this thread to the appropriate forum for a more professional support.

    Sorry for this inconvenience and thank you for your understanding.

    Best regards,

    Sara


    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


    Monday, December 4, 2017 1:38 AM
  • GetCommandLineArgs is an array, element 0 is the program with it's path, element 1 is populated when a argument is passed in e.g.

    Test it like this

    Module Module1
    
        Sub Main()
            Environment.GetCommandLineArgs() _
                .ToList _
                .ForEach(Sub(arg) Console.WriteLine(arg))
    
            Console.ReadLine()
        End Sub
    
    End Module
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, December 4, 2017 2:10 AM
    Moderator
  •  Looking at the value you set to the Command key in the registry,  if that is how you wrote it,  then it is not right.  The path and filename should have quotes around it,  and the %1 should have another set of quotes around it too.  There should be 4 quotes in all.  Here is an example of what it should look like...  note there is a single space between the quote at the end of the path and the quote before the %1.

    "c:\.......long path.....\myprogram.exe" "%1"

     You should also make sure you have set that path in the Default value of the Command key you created under your describe key.  Also make sure it is under the correct registry key for what you want.  You can look at This Link to see that if it is a Folder you are right clicking on,  then you would create your keys under the registrykey below.

    HKEY_CLASSES_ROOT\Directory\shell

     

     For example,  i created an item in the Folder right click menu called "My Command" and it opens the "MyApplication.exe" file and passes the folder path to it in the first argument (%1).

     

     Here is the code i used in the "MyApplication.exe" application i created for this example.  It has 1 label added to it to display the folder path.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim args() As String = Environment.GetCommandLineArgs
            If args.Length > 1 Then
                Label1.Text = "You clicked (My Command) on this folder:" & vbNewLine & args(1)
            End If
        End Sub
    
    End Class
     

     And here it shows the example in use.  I right click on the "Animated Circle Cursor" folder and select the "My Command" item i added to the system folder menu.  That opens my application and displays the folder path from the args.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 4, 2017 3:03 AM
    • Marked as answer by Gidmaestro Monday, December 4, 2017 10:46 AM
    Monday, December 4, 2017 3:01 AM
  • Thanks  for all the advice.  I took it and it works.
    Monday, December 4, 2017 10:47 AM