【问题标题】:use the cmd command result in case statment在 case 语句中使用 cmd 命令结果
【发布时间】:2017-03-26 09:18:35
【问题描述】:

我试图通过运行此代码从 cmd 获取 Windows 许可证状态

    Dim oProcess As New Process()
    Dim oStartInfo As New ProcessStartInfo("cmd.exe", " /c cscript ""%windir%\system32\slmgr.vbs"" /xpr | findstr ""The machine""")
    oStartInfo.CreateNoWindow = True
    oStartInfo.WindowStyle = ProcessWindowStyle.Hidden


    oStartInfo.UseShellExecute = False
    oStartInfo.RedirectStandardOutput = True
    oProcess.StartInfo = oStartInfo
    oProcess.Start()

    Dim sOutput As String
    Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
        sOutput = oStreamReader.ReadToEnd()

    End Using

    TextBox4.Text = sOutput

    Select Case sOutput
        Case Is = "The machine is permanently activated."
            TextBox4.Text = "activated"



    End Select

代码运行正常,但出现在我的 textbox4 上的结果是:机器已永久激活。我需要将此结果作为案例陈述

Select Case sOutput
        Case Is = "The machine is permanently activated."
            TextBox4.Text = "activated"

如果我得到所有我需要的机器是永久激活的。来自 cmd 的状态然后显示在 textbox4 字中(已激活)

【问题讨论】:

  • 你为什么要调用cmd来调用cscript?恕我直言,直接调用 cscript 会更有意义。我猜您的Case 中可能缺少换行符
  • 那是您得到的准确且唯一的输出吗?尝试Select Case sOutput.Trim() 删除任何前导或尾随空格/换行符。
  • 使用 visual-vincent 这是确切的代码,但我需要将结果从 sOutput 到 case 语句,如果结果 = 在 textbox4 中激活显示单词(是),如果结果 = 未激活显示单词(否)在文本框4
  • 感谢视觉文森特你是我的英雄
  • 很高兴我能再次提供帮助! :)

标签: vb.net windows shell cmd case


【解决方案1】:

可能是输出在末尾包含空格或换行符。在字符串上调用 Trim() 以删除任何前导或尾随空格和/或换行符。

Select Case sOutput.Trim()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多