使文本框声明别名

时间:2013-11-24 17:13:15

标签: vb.net

刚刚开始:有人玩过反恐精英1.6吗? 问题:您可以在其中声明别名(Alias a "+right")。 我想做同样的事情,但使用Windows cmds(Windows 7),并将命令添加到vb.net程序。 示例:alias stopit "shutdown -a"

(我把它与雨量计连接,所以盒子一直待在屏幕上) 是的,我试着谷歌,没有找到。

1 个答案:

答案 0 :(得分:0)

   [OFFTOPIC]我已经玩了很多年CS 1.6,但这有点儿   与游戏机的含义不同。[/ OFFTOPIC]

管理这个的唯一方法是在系统上设置一个新的环境变量,然后在CMD上使用环境变量,变量值应包含命令说明。

PS:当然这种更改需要CMD重置(关闭并重新打开),否则在重新打开CMD之前不能使用新的别名。

所以我们走了:

Public Class Test

    ' Textbox to set new alias.
    Private WithEvents TBSetAlias As New TextBox With
    {.Location = New Size(Me.Top, Me.Left), .Dock = DockStyle.Top}

    ' Button to save alias.
    Private WithEvents BTSaveAlias As New Button With
    {.Location = New Point(Me.Top, Me.Left + TBSetAlias.Bottom),
     .Text = "Save", .Dock = DockStyle.Fill}

    ' Parts of the alias (name, command)
    Private AliasParts As String() = {String.Empty}

    ' Name of the alias 
    Private AliasName As String = String.Empty

    ' Command of the alias 
    Private AliasCommand As String = String.Empty

    ' SendMessageTimeout Constants
    Public Const HWND_BROADCAST = &HFFFF&
    Public Const WM_SETTINGCHANGE = &H1A
    Public Const SMTO_ABORTIFHUNG = &H2

    ' SendMessageTimeout API function
    <System.Runtime.InteropServices.
    DllImport("user32.dll", SetLastError:=True)> _
    Public Shared Function SendMessageTimeout(
                  ByVal windowHandle As IntPtr,
                  ByVal Msg As Integer,
                  ByVal wParam As IntPtr,
                  ByVal lParam As String,
                  ByVal flags As SendMessageTimeoutFlags,
                  ByVal timeout As Integer,
                  ByRef result As IntPtr
    ) As IntPtr
    End Function

    ' SendMessageTimeoutFlags Enum
    <Flags()> _
    Public Enum SendMessageTimeoutFlags As Integer
        SMTO_NORMAL = &H0
        SMTO_BLOCK = &H1
        SMTO_ABORTIFHUNG = &H2
        SMTO_NOTIMEOUTIFNOTHUNG = &H8
    End Enum

    Private Shadows Sub Load() Handles MyBase.Load

        ' Load the controls into UI
        Me.Controls.AddRange({TBSetAlias, BTSaveAlias})

        ' Set the textbox text alias
        TBSetAlias.Text = "alias dirfiles ""dir /B *.*"""

    End Sub

    Private Sub TBSetAlias_TextChanged(sender As Object, e As EventArgs) _
    Handles TBSetAlias.TextChanged

        AliasParts = sender.text.split

    End Sub

    Private Sub BTSaveAlias_Click() Handles BTSaveAlias.Click

        If AliasParts Is Nothing Then
            Exit Sub

        ElseIf AliasParts.Count > 1 Then

            Select Case AliasParts.FirstOrDefault.ToLower.Trim

                Case "alias"
                    AliasName = AliasParts(1)
                    AliasCommand = String.Join(Convert.ToChar(Keys.Space),
                                               AliasParts.Skip(2)).
                                               Replace(ControlChars.Quote, String.Empty)

                    ' Set an environment variable
                    SetEnvironmentVariable(AliasName, AliasCommand, Microsoft.Win32.Registry.CurrentUser)

                Case "else"
                    ' Is not a valiad alias
                    Throw New Exception("Invalid Alias.")

            End Select

        End If

    End Sub

    Public Sub SetEnvironmentVariable(ByVal VariableName As String,
                                      ByVal Value As String,
                                      ByVal RootKey As Microsoft.Win32.RegistryKey)

        Select Case RootKey.Name

            Case Microsoft.Win32.Registry.CurrentUser.Name
                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Environment",
                                              VariableName,
                                              Value,
                                              Microsoft.Win32.RegistryValueKind.String)

            Case Microsoft.Win32.Registry.CurrentUser.Name
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
                                              VariableName,
                                              Value,
                                              Microsoft.Win32.RegistryValueKind.String)
        End Select

        ' Update System Change
        SendMessageTimeout(HWND_BROADCAST, _
                           WM_SETTINGCHANGE, _
                           0, _
                           "Environment", _
                           SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, _
                           100, _
                           IntPtr.Zero)

    End Sub

End Class