2017-12-30 4 views
2

cmd에서 배터리 정보를 읽으려고합니다. 아래 그림과 같이 각 값을 추출하여 각각의 텍스트 상자에 넣을 수 있습니까?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 표지 할 것 :

User Interface

여기 내 코드입니다.

+0

데이터가 TextBox에 저장되는 방법을 보여줄 수 있습니다. 그러나 VbCrLf를 구분 기호로 사용하여 TextBox.Text를'.Split() '해야합니다. – Jimi

+0

지금까지 어떤 코드를 작성 했습니까? 우리는 당신을 위해 전체 코드를 간단하게 작성할 수는 없습니다 – Sunil

+0

모든 관련 정보는 다른 사이트에 링크되어 있지 않은 질문에 게시되어야합니다. –

답변

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 

자신이 앱에서 사용하는 레이블과 텍스트 상자로 이루어진 사용자 정의 컨트롤로 만들 수 있습니다. 그런 다음 목록의 각 항목을 사용자 지정 컨트롤에 전달하기 만하면됩니다. 쉽게 추가하거나 IndexOf(), Substring() 방법과 Length 속성을 사용하여 새 라인 (Enviroment.NewLine)로 구분 된 텍스트에서 값을 추출하는 방법 중 하나를 다음 IF의 톤을하지 않고 필드를 제거하거나 큰 선택 케이스 여기

3

할 수 있습니다 :

당신의 텍스트 상자가 수익 문자열 값을 똑같이 이름을 지정하지 않는 한
Private Sub UpdateGUI() 
    Dim lines As String() = TextBox1.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) 

    For Each itm In lines 
     MsgBox(itm.Substring(itm.IndexOf(":") + 1, itm.Length - itm.IndexOf(":") - 1)) 
    Next 
End Sub 
+1

멋지고 짧고 간결한 솔루션! 나는 Regex를 사용하여 답을 쓰려고 생각하고 있었지만, 너무 복잡해 졌을 수도 있습니다. –

+1

아마도 'StringSplitOptions.RemoveEmptyEntries'가 여기에 더 적합 할 수도 있습니다. 그냥 예방 조치. – Jimi

+0

@ 지미 나는 'StringSplitOptions.RemoveEmptyEntries', tnx를 사용하는 것이 더 나을 것이라고 생각합니다. – jonathana

2

정말이 수동으로 각 코드를 제외 할 수있는 방법이없는, (당신은 어쨌든 다른 문자로 공간을 변환해야 할 것이다) 케이스. 그것을해야 이런 식으로 뭔가, 그냥 따라 텍스트 상자의 이름을 변경 :

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 

당신이 예측 가능한 방식으로 텍스트 상자라는 이름의 경우에, 당신은 Controls.Find()를 사용하고 아래에 코드를 단축 할 수 있습니다. 예를 들어 공백을 밑줄로 바꾸고 앞에 "txt"를 붙입니다 : "txtAC_Powered", "txtUSB_Powered", "txtStatus", "txtHealth"등 대소 문자는 상관 없습니다. 즉, 긴 이름의 성명서 (또는 다른 것)를 작성하는 것과 대조적으로 컨트롤의 이름을 지정하는 것과 같은 수동 작업을 수행한다는 의미입니다.

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 
관련 문제