Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, October 5, 2017 1:16 AM
Hello everyone, I kindly ask for an orientation in my code, which is done in C # with Android Debug Bridge (ADB) commands that run at a DOS prompt.
The command basically reads the serial number from my tablet and transfers it to a textBox. Until then, no problem. However I need to copy this serial number from this textBox and transfer it to a new ADB command in DOS to read the battery data. Below is the code for better understanding, the first block is to get the serial number and transfer it to textBox1:
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "adb.exe";
startInfo.Arguments = "get-serialno";
process.StartInfo = startInfo;
process.Start();
string output1 = process.StandardOutput.ReadToEnd();
textBox1.Text = output1;
process.WaitForExit();
}
And the second block is the code that will direct the commands to the serial number:
startInfo.FileName = "adb.exe";
startInfo.Arguments = "-s " + textBox1.Text + " shell dumpsys battery";
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
richTextBox1.Text = output;
process.WaitForExit();
But I can not get it to recognize the variable in the argument line. I've tried to concatenate, without success ... where am I going wrong? What I've already tried, in the searches I've done:
startInfo.Arguments = String.Format(@"/c adb -s ""{0}"" shell dumpsys battery", output1);
startInfo.Arguments = "/c adb -s \"" + output1 + "\" shell dumpsys battery";
startInfo.Arguments = $@"/c adb -s ""{output1}"" shell dumpsys battery";
Thanks!!!
All replies (4)
Thursday, October 5, 2017 5:55 AM
Hello hideki_japs,
I'm not proficient in android technology,the following just is my some suggestion.
1.you should check the command line if works well in cmd by manual input.
2.you could try to use alternative code like below.
Process.Start(FilePath, @"-a arg1 -b arg2");
3.there is a good example for how to send series of commands to a process.
Sincerely,
neil hu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Thursday, October 5, 2017 3:01 PM
The variants of the argument seem fine but you should be using \ and $ and not @.
startInfo.Arguments = $"/c adb -s \"{output1}\" shell dumpsys battery";
I would verify the Arguments property after you set it in the debugger. You should also try taking that command line and running it directly in a command prompt. If it doesn't work in the command prompt then it is wrong. What seems suspect to me is that you're using /c adb which tells me you might be trying to call cmd.exe and passing the /c adb as the command to execute. But you're setting FileName to adb directly. So the command wouldn't work. I would set FileName as adb.exe and then remove the /c adb from the arguments as they aren't needed. If you do want to use cmd.exe then change the FileName to be cmd.exe instead. Also note that adb.exe would need to be in the path otherwise it would not be found. Posting the exception you get or the error code from the process would shed some light on what is going wrong.
Michael Taylor
https://www.michaeltaylorp3.net
Friday, October 6, 2017 11:17 AM
Tried this command, but with no success at all.
Friday, October 6, 2017 1:30 PM
Please be specific in what you tried given that there were several options given. Also please provide the exact error(s) you're getting. The more information you give us up front the faster this problem can be resolved.