我正在尝试从cmd读取电池信息。如何提取每个值并将它们放在各自的文本框中,如下图所示?
这是我的代码:
Private Results As String
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Private Sub UpdateText()
TextBox11.Text = Results
End Sub
Private Sub batt_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = False 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("adb shell dumpsys battery") 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As String = TextBox11.Text
Dim topic_start As String = topic_start = value.LastIndexOf("AC Powered:") + 1
Dim topic As String = value.Substring(topic_start, value.Length - topic_start)
TextBox1.Text = topic.ToString
End Sub
Button1
在图片中标记为Get Battery Information
。
答案 0 :(得分:3)
这是一种使用class Store {
@action firstFunction() {
console.log("First Function!")
this.secondFunction();
}
@action secondFunction(){
console.log("Second Function!");
}
}
const store = new Store();
console.log("This works:");
store.firstFunction();
console.log("This will throw an error:");
setTimeout(store.firstFunction, 1000);
,Enviroment.NewLine
方法和IndexOf()
属性从新行(Substring()
)分隔的文本中提取值的方法:
Length
答案 1 :(得分:2)
Private Sub UpdateText()
Dim xList As New List(Of KeyValuePair(Of String, String))
Results = Results.Replace(vbLf, "")
Dim LineSplit() As String = Results.Split(vbCr)
For Each xLine As String In LineSplit
If xLine <> "" Then
xList.Add(New KeyValuePair(Of String, String)(xLine.Split(":")(0), xLine.Split(":")(1).Trim.Replace(" ", "")))
End If
Next
'do some work here to put the values in the right textboxes
End Sub
您可以自己制作一个由您在应用中使用的标签和文本框组成的自定义控件。然后,您只需将列表的每个条目传递给自定义控件。这样可以更轻松地添加或删除字段,而无需使用大量的IF或大的Select Case
答案 2 :(得分:2)
除非您的TextBoxes的名称与返回字符串中的值完全相同(您必须将空格转换为其他字符),除了手动编码外,实际上没有办法做到这一点每个案例。这样的事情应该这样做,只需相应地更改TextBoxes的名称:
Private Sub UpdateText()
Dim lines() As String = Results.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
For Each line In lines
Dim values() As String = line.Split(":")
Select Case values(0).ToUpper.Trim
Case "AC POWERED"
TextBox1.Text = values(1).ToUpper.Trim
Case "USB POWERED"
TextBox2.Text = values(1).ToUpper.Trim
Case "WIRELESS POWERED"
TextBox3.Text = values(1).ToUpper.Trim
Case "STATUS"
TextBox4.Text = values(1).ToUpper.Trim
Case "HEALTH"
TextBox5.Text = values(1).ToUpper.Trim
Case "PRESENT"
TextBox6.Text = values(1).ToUpper.Trim
Case "LEVEL"
TextBox7.Text = values(1).ToUpper.Trim
Case "SCALE"
TextBox8.Text = values(1).ToUpper.Trim
Case "VOLTAGE"
TextBox9.Text = values(1).ToUpper.Trim
Case "TEMPERATURE"
TextBox10.Text = values(1).ToUpper.Trim
Case "TECHNOLOGY"
TextBox11.Text = values(1).ToUpper.Trim
Case Else
MessageBox.Show(line, "Unknown Value")
End Select
Next
End Sub
如果您以可预测的方式命名TextBoxes,则可以使用Controls.Find()
并将代码缩短到下面。例如,将空格更改为下划线,并添加&#34; txt&#34;在前面:&#34; txtAC_Powered&#34;,&#34; txtUSB_Powered&#34;,&#34; txtStatus&#34;,&#34; txtHealth&#34;等。案例无关紧要,因为匹配将是发现不管。这意味着您在命名控件时进行手动工作,而不是编写长选择语句(或其他内容):
Private Sub UpdateText()
Dim lines() As String = Results.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
For Each line In lines
Dim values() As String = line.Split(":")
Dim ctlName As String = "txt" & values(0).Replace(" ", "_").Trim
Dim ctl As Control = Me.Controls.Find(ctlName, True).FirstOrDefault
If Not IsNothing(ctl) Then
ctl.Text = values(1).ToUpper.Trim
End If
Next
End Sub