Search code examples
vb.netregistrycontextmenu

Variables cant get embedded


I am trying to write a programm that adds given Apps (already done) and custom URLs to the context menu on the desktop

When I run the programm and choose custom, enter the needed parameters, it creates the needed Launcher Batch script the registry key but the variable givenName which defines the Name wasnt added and the files are called ".bat" or the first Key doesnt gets generated (which needs the name).

the same is going on with the URL that needs to be saved in the Batch script to launch the chosen URL

This is the Code for the Form where this is happening:

Public Class FormCustom
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Hide()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim DirExists As Boolean = Nothing
        If My.Computer.FileSystem.DirectoryExists("C:\ShortCut") Then
            DirExists = True
        End If
        If DirExists = False Then
            My.Computer.FileSystem.CreateDirectory("C:\ShortCut")
        End If
        Dim Position As String = Nothing
        If RadioButton1.Checked Then
            Position = "Middle"
        Else
            If RadioButton2.Checked Then
                Position = "Bottom"
            End If
        End If
        Dim givenName As String = Nothing
        Dim givenURL As String = Nothing
        TextBox2.Text = givenURL
        TextBox1.Text = givenName
        Dim sb As New System.Text.StringBuilder
        sb.AppendLine("@echo off")
        sb.Append("start " + givenURL)
        IO.File.WriteAllText("C:\ShortCut\" + givenName + ".bat", sb.ToString())
        My.Computer.Registry.ClassesRoot.CreateSubKey("DesktopBackground\Shell\" + givenName)
        My.Computer.Registry.ClassesRoot.CreateSubKey("DesktopBackground\Shell\" + givenName + "\command")
        My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName, True).SetValue("(Default)", "@shell32.dll")
        My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName + "\command", True).SetValue("(Default)", "@shell32.dll")
        My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName, True).SetValue("icon", "explorer.exe")
        My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName, True).SetValue("Position", Position)
        My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName + "\command", True).SetValue("(Default)", "C:\ShortCut\" + givenName + ".bat")
    End Sub
End Class

I tried to add the variable with a "+" and know really know why it doesnt accept it

[SOLVED] Availible on GitHub: https://github.com/amir00t/LvL-up


Solution

  • This sets the text boxes' text to your variables:

    TextBox2.Text = givenURL
    TextBox1.Text = givenName
    

    You want it the other way around:

    givenURL = TextBox2.Text
    givenName = TextBox1.Text
    

    Variables are assigned like this:

    <variable to set> = <value to give the variable>
    

    For example:

    Dim MyString1 As String = "1"
    Dim MyString2 As String = "2"
    Dim MyString3 As String = "3"
    
    MyString1 = "Hello!"       'Sets "MyString1" to "Hello!".
    MyString3 = MyString2      'Sets "MyString3" to the value of "MyString2".
    MyString2 = "This is text" 'Sets "MyString2" to "This is text"
    
    MessageBox.Show(MyString1) 'Shows: Hello!
    MessageBox.Show(MyString2) 'Shows: This is text
    MessageBox.Show(MyString3) 'Shows: 2
    
    "Oh hi!" = MyString3       'Syntax error.
    

    EDIT:

    To set the value of the (Default) registry key you shall just specify an empty string for the value name.

    For example:

    My.Computer.Registry.ClassesRoot.OpenSubKey("DesktopBackground\Shell\" + givenName, True).SetValue("", "@shell32.dll")
    

    Notice how there's no text in the first parameter of SetValue:

    .SetValue("", "@shell32.dll")
    '         ^ Empty string.
    

    EDIT 2:

    Just so you know, there is the ElseIf keyword that you can use to specify an alternative check in your If-statement:

    If RadioButton1.Checked Then
        Position = "Middle"
    ElseIf RadioButton2.Checked Then
        Position = "Bottom"
    End If
    

    Usage:

    If condition1 Then
        'Code...
    ElseIf condition2 Then
        'Code...
    ElseIf condition3 Then
        'Code...
    ElseIf condition... Then
        'Code...
    End If
    

    More info on the MSDN.