【问题标题】:Variables cant get embedded变量无法嵌入
【发布时间】:2017-04-21 13:06:16
【问题描述】:

我正在尝试编写一个程序,将给定的应用程序(已经完成)和自定义 URL 添加到桌面上的上下文菜单中

当我运行程序并选择自定义时,输入所需的参数,它会创建所需的 Launcher Batch 脚本和注册表项,但未添加定义名称的变量 givenName 并且文件称为“.bat”或第一个键不会生成(需要名称)。

同样需要保存在批处理脚本中以启动所选 URL 的 URL

这是发生这种情况的表单的代码:

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

我尝试用“+”添加变量并且知道它为什么不接受它

[已解决] 可在 GitHub 上找到:https://github.com/amir00t/LvL-up

【问题讨论】:

    标签: vb.net registry contextmenu


    【解决方案1】:

    这会将文本框的文本设置为您的变量:

    TextBox2.Text = givenURL
    TextBox1.Text = givenName
    

    你想要反过来:

    givenURL = TextBox2.Text
    givenName = TextBox1.Text
    

    变量的分配方式如下:

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

    例如:

    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.
    

    编辑:

    要设置(Default) 注册表项的值,您只需为值名称指定一个空字符串。

    例如:

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

    注意SetValue的第一个参数中没有文本:

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

    编辑 2:

    请注意,ElseIf 关键字可用于在 If-statement 中指定替代检查:

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

    用法:

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

    有关MSDN的更多信息。

    【讨论】:

    • 很抱歉让您提出一个新问题,但在看到代码之前,我不可能知道问题是如此简单。 :)
    • 没问题,但我想我发现了另一件事我应该先解决
    • 我没有修复它,但批处理脚本没有保存在注册表项的默认位置,而是自己制作一个
    • 如果它在(默认)键(德语:(标准))中,那没问题,它会像魅力一样工作
    • @Max:很高兴我能帮上忙!很抱歉给您带来不便;)。祝你的项目好运!
    猜你喜欢
    • 2014-01-15
    • 2012-01-30
    • 2015-09-10
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2014-11-25
    • 2018-12-23
    • 2015-04-19
    相关资源
    最近更新 更多