-1

I'm at my wits end on this. So, I just want to build an app in VB.net that uses the adb (Android Debug Bridge) to get information about a device. Currently, I'm trying to get the device's brand using the command: adb shell getprop ro.product.brand which should return Amazon. However, when I try to use an if statement to see if the output of the adb command equals Amazon it always returns an error (stating it isn't an Amazon device). I've tried so much to get this worked and I cannot figure it out for the life of me. Does anyone have any suggestions on how this can be fixed? Am I missing something?

Public Class Form1
    Function adb(ByVal Arguments As String) As String
        Try
            Dim My_Process As New Process()
            Dim My_Process_Info As New ProcessStartInfo()

            My_Process_Info.FileName = "cmd.exe" 
            My_Process_Info.Arguments = Arguments 
            My_Process_Info.WorkingDirectory = "adb\"
            My_Process_Info.CreateNoWindow = True  
            My_Process_Info.UseShellExecute = False
            My_Process_Info.RedirectStandardOutput = True
            My_Process_Info.RedirectStandardError = True 
            My_Process.EnableRaisingEvents = True 
            My_Process.StartInfo = My_Process_Info
            My_Process.Start()

            Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() 
            Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd()
            Console.WriteLine(Process_ErrorOutput)
            Console.WriteLine(Process_StandardOutput)

            ' Return output by priority
            If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput 
            If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput
        Catch ex As Exception
            Return ex.Message
        End Try

        Return "OK"
    End Function

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim BrandofDev

        BrandofDev = (adb("/c adb shell getprop ro.product.brand"))

        If BrandofDev = "Amazon" Then
            MsgBox("This is an amazon device.")
        Else
            MsgBox("This isn't an Amazon device.")
        End If
    End Sub
End Class
MatSnow
  • 7,132
  • 3
  • 20
  • 31
  • When you print `Process_StandardOutput` and `Process_ErrorOutput`, what those strings look like? Have you tried to set the working directory to a full path? (remove `My_Process.EnableRaisingEvents = True`, add `My_Process.WaitForExit()`). – Jimi Dec 30 '19 at 09:48
  • @Jimi I tried making the changes you recommended and still no good. The console prints both error and standard output. The standard output is empty while the error output has the Amazon variable. So strange... – DataStream3 Dec 30 '19 at 10:07
  • It's not *strange*. You can find similar cases quite often: stdErr is written/flushed instead of stdOut. But it doesn't matter here, since you're returning one or the other, but stdErr first. What is the **exact** string returned? (A full path is required: `"adb\"` may work while using Visual Studio or when your app is first started, no guaratees it will work after). – Jimi Dec 30 '19 at 10:10
  • It is throwing an exception now: Exception thrown: 'System.InvalidOperationException' in System.dll – DataStream3 Dec 30 '19 at 10:14
  • What is throwing an exception? Which line? Which instruction? Are you debugging this code or just waiting for the method to return something? – Jimi Dec 30 '19 at 10:17
  • 1
    @DataStream3 You forgot the `As String` on the line `Dim BrandofDev`. To prevent errors like that, use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360). – Andrew Morton Dec 30 '19 at 11:31
  • Use a message box to preview the value of `BrandOfDev`. What do you get? – preciousbetine Dec 30 '19 at 12:59
  • @preciousbetine the value is Amazon. – DataStream3 Dec 30 '19 at 21:03
  • 1
    @Jimi I managed to fix the exception by fixing the path to Adb. – DataStream3 Dec 30 '19 at 21:27
  • Figured it out. Apparently when executing any command in adb it ouputs the value and a blank line after it. So the value wasn't just Amazon. – DataStream3 Jan 01 '20 at 10:00
  • What I ended up doing was I used the .TrimEnd() to remove the extra line break. Everything is working as intended now. Thank you all for the help! – DataStream3 Jan 01 '20 at 10:54

1 Answers1

1

I am probably missing the point but...

Private Sub OpCode()
    '****They are both the same thing so, of course they both print
    Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd()
    Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd()
    Console.WriteLine(Process_ErrorOutput)
    Console.WriteLine(Process_StandardOutput)

    ' Return output by priority
    '****The Process_ErrorOutput will always be the one returned 
    '(but what does it matter; they are both the same) Or Nothing is returned
    If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput
    If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput
End Sub

EDIT As per comment by @Jimi Reguarding the .WorkingDirectory here is the relevant link https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.workingdirectory?view=netframework-4.8

Mary
  • 14,731
  • 3
  • 19
  • 27
  • I missed that. I just considered `Console.WriteLine(Process_ErrorOutput)`, while the two strings are read from the same Stream - well, one is.... Since you're *already here*, you could write a note about the WorkingDirectory, since setting it to a *real* path [fixed the exception](https://stackoverflow.com/questions/59528550/if-statement-returning-as-false-when-it-should-be-true#comment105243771_59528550). – Jimi Dec 31 '19 at 00:29
  • @Jimi I added a link. Apparently the OP has fixed his problem by fixing the path as per your comment. – Mary Dec 31 '19 at 04:38