通过手机远程关机

来源:互联网 发布:淘宝刷单惩罚 编辑:程序博客网 时间:2024/04/25 08:36

通过手机远程关机http://www.cnblogs.com/

http://www.cnblogs.com/

通过手机进行远程关机

主要流程

1.申请一个博客园账号,找到“我的博客园”页面,拷贝url

2.手机发送“随笔” 内容是“关机”

3.程序每隔五分钟检测一下url上有没有当前的关机命令

4.执行关机命令

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using System.IO.Compression;using System.Text.RegularExpressions;using System.Diagnostics;namespace 自动关机{    class Program    {        static void Main(string[] args)        {            //命令格式 “关机”            int wait = 1000* 60 * 5; //五分钟执行一次            string url = "http://www.cnblogs.com/mengxingxinqing/";//配置自己的博客园地址            string regx = @"关机";            string timereg = @"\d{4}-\d{2}-\d{2} \d{2}:\d{2}";            while (true)            {                Regex r = new Regex(regx);                string html = getPageInfo(url);                //Console.Write(html);                MatchCollection mc;                mc = r.Matches(html);                if (mc.Count > 0)                {                    string tmp = mc[0].Value;                    tmp = html.Substring(mc[0].Index, 100);                    tmp = tmp.Replace("&nbsp;", " ");                    Regex tr = new Regex(timereg);                    MatchCollection tmc;                    tmc = tr.Matches(tmp);                    tmp = tmc[0].Value;                    string datestr = tmp + ":00";                    DateTime dt = Convert.ToDateTime(datestr);                    DateTime now = DateTime.Now;                    if (abs(DateDiff("tms", now, dt)) < 2*wait)                    {                        //Console.Write("shutdown");                        Cmd("shutdown -s");                    }                }                System.Threading.Thread.Sleep(wait);            }        }        /// <summary>        /// 请求Url并获取返回值        /// </summary>        /// <param name="strUrl">Url地址</param>        /// <returns></returns>        public static string getPageInfo(string strUrl)        {            WebClient wc = new WebClient(); // 创建WebClient实例提供向URI 标识的资源发送数据和从URI 标识的资源接收数据            wc.Credentials = CredentialCache.DefaultCredentials; // 获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。            ///方法一:            Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312            Byte[] pageData = wc.DownloadData(strUrl); // 从资源下载数据并返回字节数组。            string shtml = enc.GetString(pageData);            return shtml;        }        /// <summary>           /// 返回两个日期之间的时间间隔(y:年份间隔、M:月份间隔、【d:天数间隔、h:小时间隔、m:分钟间隔、s:秒钟间隔、ms:微秒间隔,中括号内前加t,表示总数,如td,总天数】)           /// </summary>           /// <param name="Interval">间隔标志</param>         /// <param name="Date1">开始日期</param>           /// <param name="Date2">结束日期</param>                     /// <returns>返回间隔标志指定的时间间隔</returns>           public static double DateDiff(string Interval, System.DateTime? Date1, System.DateTime? Date2)        {            double dblYearLen = 365;//年的长度,365天               double dblMonthLen = (365 / 12);//每个月平均的天数               System.TimeSpan objT;            DateTime d1 = new DateTime();            DateTime d2 = new DateTime();            if (Date1 == null) return 0;            if (Date2 == null) return 0;            d1 = (DateTime)Date1;            d2 = (DateTime)Date2;            objT = d2.Subtract(d1);            switch (Interval)            {                case "y"://返回日期的年份间隔                       return (double)System.Convert.ToInt32(objT.Days / dblYearLen);                case "M"://返回日期的月份间隔                       return (double)System.Convert.ToInt32(objT.Days / dblMonthLen);                case "d"://返回日期的天数间隔                      objT = Convert.ToDateTime(d2.ToShortDateString()).Subtract(Convert.ToDateTime(d1.ToShortDateString()));                    return (double)objT.Days;                case "h"://返回日期的小时间隔                       return (double)objT.Hours;                case "m"://返回日期的分钟间隔                       return (double)objT.Minutes;                case "s"://返回日期的秒钟间隔                       return (double)objT.Seconds;                case "ms"://返回时间的微秒间隔                       return (double)objT.Milliseconds;                case "td"://总天                    return objT.TotalDays;                case "th"://总小时数                    return objT.TotalHours;                case "tm"://总分钟                    return objT.TotalMinutes;                case "ts"://总秒                    return objT.TotalSeconds;                case "tms"://总毫秒                    return objT.TotalMilliseconds;                default:                    break;            }            return 0;        }        /// <summary>        /// 执行命令行        /// </summary>        /// <param name="cmd"></param>        /// <returns></returns>        public static string Cmd(string command)        {            string output = ""; //输出字符串              if (command != null && !command.Equals(""))            {                Process process = new Process();//创建进程对象                  ProcessStartInfo startInfo = new ProcessStartInfo();                startInfo.FileName = "cmd.exe";//设定需要执行的命令                  startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出                  startInfo.UseShellExecute = false;//不使用系统外壳程序启动                  startInfo.RedirectStandardInput = false;//不重定向输入                  startInfo.RedirectStandardOutput = true; //重定向输出                  startInfo.CreateNoWindow = true;//不创建窗口                  process.StartInfo = startInfo;                try                {                    if (process.Start())//开始进程                      {                        process.WaitForExit();//这里无限等待进程结束                          output = process.StandardOutput.ReadToEnd();//读取进程的输出                      }                }                catch                {                }                finally                {                    if (process != null)                        process.Close();                }            }            return output;                  }        public static double abs(double d)        {            return d > 0 ? d : -d;        }    }}
复制代码

在vs中编译后,生成可执行文件,将可执行文件创建一个快捷方式,将快捷方式放到C:\Documents and Settings\Administrator\「开始」菜单\程序\启动 中便可以开机自启动了

分类: asp.net