-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMD.cs
97 lines (78 loc) · 2.67 KB
/
CMD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace RelayController
{
class CMD
{
//global - adb timeout in miliseconds
static int timeout = 900000;
//global - main class
Relay mw = null;
public CMD(Relay mw)
{
this.mw = mw;
}
//this method handles the actual sending of the command to shell
//no putput redirection. executed in a seperate window.
public string Execute(string command)
{
Process p = new Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + command;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
p.Start();
string adbResponse = p.StandardOutput.ReadToEnd();
adbResponse += p.StandardError.ReadToEnd();
if (!p.WaitForExit(timeout))
{
return string.Empty;
}
else
{
return adbResponse;
}
}
//executes a cli command and returns a string with it's result.
public int ExecuteRespondingExitCode(string command)
{
Process p = new Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + command;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
p.Start();
string adbResponse = p.StandardOutput.ReadToEnd();
adbResponse += p.StandardError.ReadToEnd();
if (!p.WaitForExit(timeout))
{
return 0 ;
}
else
{
return p.ExitCode ;
}
}
//private void cmd_DataReceived(object sender, DataReceivedEventArgs e)
//{
// mw.listBox1.Invoke(new Action(() => { mw.listBox1.Items.Add(e.Data); }));
//}
//private void cmd_Error(object sender, DataReceivedEventArgs e)
//{
// mw.listBox1.Invoke(new Action(() => { mw.listBox1.Items.Add(e.Data); }));
//}
}
}