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
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
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小時內刪除侵權內容。