RemoteHelper V1.0 (远程关机助手)-----源代码放出

网友投稿 562 2022-05-25

某天上课的时候,老湿说了下利用微薄关电脑的功能,自己从实用性,原理性,实现性,待加强四个方面进行了对比

RemoteHelper V1.0 (远程关机助手)-----源代码放出

实用性

1.使用场景:一般情况下,好像使用软件都是在下载未完成的情况下使用的,其实有下载并关机的功能的软件,有太多了,比如,讯雷的

又比如115网盘的下载关机功能

其实内置下载关机的功能相当多。反而单独将远程关机这个功能拿出来的很少很少

原理性

手机发出特征信息(1)——>软件收到特征信息(2)——>软件执行关机功能(3)

RemoteHelper V1.0 直接利用正则表达式(真是感叹,正则表达式功能何其的强大!),执行步骤(2),然后执行功能(3)

实现性

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Management; using System.Diagnostics; using System.Net; using System.Text.RegularExpressions; using System.IO; using System.Timers; using System.Threading; namespace Test1 { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)] private static extern int ExitWindowsEx(int uFlags, int dwReserved); //定义为全局变量 //记录list的索引 int x = -1; //定义全局的内容 List list = new List(); Thread thGet; private void button1_Click(object sender, EventArgs e) { //重新设定索引序列的值 x = -1; //将lbState的值重新清空 lbState.Items.Clear(); //添加当前状态 lbState.Items.Add("线程初使化,执行监听..."); btnStart.Enabled = false; btnCancel.Enabled = true; thGet = new Thread(OffByNet); //如果不设置都所有线程都是前台,在前台闭后然后执行关 thGet.IsBackground = true; thGet.Start(txtUrl.Text.Trim()); } private void btnCancel_Click(object sender, EventArgs e) { btnStart.Enabled = true; btnCancel.Enabled = false; if (thGet!=null) { thGet.Abort(); } } //执行关机方法 #region 关机 void shutdown() { Process myProcess = new System.Diagnostics.Process(); myProcess.StartInfo.FileName = "cmd.exe"; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); myProcess.StandardInput.WriteLine("shutdown -s -t 60"); } #endregion //获取网页 private void GetContent(string url) { StringBuilder sb = new StringBuilder(); WebRequest wr = WebRequest.Create(url); WebResponse wre = wr.GetResponse(); using (Stream str = wre.GetResponseStream()) { StreamReader sr = new StreamReader(str, Encoding.Default); string line; while ((line = sr.ReadLine()) != null) { sb.Append(line); } } list.Add(sb.ToString()); } //反应状态 private bool CommandResult(string content, string command) { bool result = false; Match ma = Regex.Match(content, command); if (ma.Success) { result = true; } return result; } //读文本关机 private void GetBytxt() { while (true) { Thread.Sleep(2000); x++; string content = File.ReadAllText("1.txt", Encoding.Default); list.Add(content); bool b = CommandResult(list[x], txtCommand.Text.Trim()); if (b==true) { shutdown(); } } } //读网络关机 private void OffByNet(object ur) { string url = ur as string; while (true) { Thread.Sleep(2000); x++; GetContent(url); bool result = CommandResult(list[x], txtCommand.Text.Trim()); if (result==true) { lbState.Items.Clear(); lbState.Items.Add("捕获命令,执行关机"); shutdown(); thGet.Abort(); } else { lbState.Items.Add("正在监听中,请等待......."); } } } private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dr = MessageBox.Show("是否取消当前监听?","监听",MessageBoxButtons.YesNo,MessageBoxIcon.Warning); if (dr !=System.Windows.Forms.DialogResult.Yes) { e.Cancel = true; } } private void FrmMain_Load(object sender, EventArgs e) { } } }

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

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Management;

using System.Diagnostics;

using System.Net;

using System.Text.RegularExpressions;

using System.IO;

using System.Timers;

using System.Threading;

namespace Test1

{

public partial class FrmMain : Form

{

public FrmMain()

{

InitializeComponent();

Control.CheckForIllegalCrossThreadCalls = false;

}

[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]

private static extern int ExitWindowsEx(int uFlags, int dwReserved);

//定义为全局变量

//记录list的索引

int x = -1;

//定义全局的内容

List list = new List();

Thread thGet;

private void button1_Click(object sender, EventArgs e)

{

//重新设定索引序列的值

x = -1;

//将lbState的值重新清空

lbState.Items.Clear();

//添加当前状态

lbState.Items.Add("线程初使化,执行监听...");

btnStart.Enabled = false;

btnCancel.Enabled = true;

thGet = new Thread(OffByNet);

//如果不设置都所有线程都是前台,在前台闭后然后执行关

thGet.IsBackground = true;

thGet.Start(txtUrl.Text.Trim());

}

private void btnCancel_Click(object sender, EventArgs e)

{

btnStart.Enabled = true;

btnCancel.Enabled = false;

if (thGet!=null)

{

thGet.Abort();

}

}

//执行关机方法

#region 关机

void shutdown()

{

Process myProcess = new System.Diagnostics.Process();

myProcess.StartInfo.FileName = "cmd.exe";

myProcess.StartInfo.UseShellExecute = false;

myProcess.StartInfo.RedirectStandardInput = true;

myProcess.StartInfo.RedirectStandardOutput = true;

myProcess.StartInfo.RedirectStandardError = true;

myProcess.StartInfo.CreateNoWindow = true;

myProcess.Start();

myProcess.StandardInput.WriteLine("shutdown -s -t 60");

}

#endregion

//获取网页

private void GetContent(string url)

{

StringBuilder sb = new StringBuilder();

WebRequest wr = WebRequest.Create(url);

WebResponse wre = wr.GetResponse();

using (Stream str = wre.GetResponseStream())

{

StreamReader sr = new StreamReader(str, Encoding.Default);

string line;

while ((line = sr.ReadLine()) != null)

{

sb.Append(line);

}

}

list.Add(sb.ToString());

}

//反应状态

private bool CommandResult(string content, string command)

{

bool result = false;

Match ma = Regex.Match(content, command);

if (ma.Success)

{

result = true;

}

return result;

}

//读文本关机

private void GetBytxt()

{

while (true)

{

Thread.Sleep(2000);

x++;

string content = File.ReadAllText("1.txt", Encoding.Default);

list.Add(content);

bool b = CommandResult(list[x], txtCommand.Text.Trim());

if (b==true)

{

shutdown();

}

}

}

//读网络关机

private void OffByNet(object ur)

{

string url = ur as string;

while (true)

{

Thread.Sleep(2000);

x++;

GetContent(url);

bool result = CommandResult(list[x], txtCommand.Text.Trim());

if (result==true)

{

lbState.Items.Clear();

lbState.Items.Add("捕获命令,执行关机");

shutdown();

thGet.Abort();

}

else

{

lbState.Items.Add("正在监听中,请等待.......");

}

}

}

private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)

{

DialogResult dr = MessageBox.Show("是否取消当前监听?","监听",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);

if (dr !=System.Windows.Forms.DialogResult.Yes)

{

e.Cancel = true;

}

}

private void FrmMain_Load(object sender, EventArgs e)

{

}

}

}

不足性

微薄关机,监控软件始终需要一个公共的参看微薄的功能,软件在写的时候一直没有解决这个”公共的查看微薄”的功能,如果不登录,本身是没有查看的功能。思来想去,要么微薄提供一个公共的API,要不然就用其它的方式,想了半天还是没有想出解决方法,今天在cnblogs看到一位朋友说了解决方法:

注册一个公共的微薄账号,然后关注需要监控的微薄,将公共微薄内置在监控软件内(可是如何实现软件模拟登陆呢?),然后就可以实现远程关机功能

小弟初学C#,欢迎各位大神拍砖

正则表达式

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:YOLOv4 物体/目标检测之实战篇(Windows系统、Python3、TensorFlow2版本)
下一篇:51单片机蓝牙小车
相关文章