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

实用性

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

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

原理性

实现性

    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<string> list = new List<string>();
            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#,欢迎各位大神拍砖