• 大小: 58KB
    文件类型: .rar
    金币: 2
    下载: 0 次
    发布日期: 2024-02-03
  • 语言: C#
  • 标签: UPS  

资源简介

服务器如果经常非法关机则很空易损坏系统,特别是使用软镜像或RAID-5的,每次非法关机后就需要重新同步,特别损坏硬盘。想偷懒从网上下载同类工具,却都有毒,真TMD不厚道,被逼自行开发,使用C#。 文件说明: ServerAutoShutDown.exe---服务程序 SRVINSTW.EXE--服务安装工具,使用方法请网上查 Config.ini--配置文件 IP---要检查的IP地址,建议与UPS同一市电插座的路由器 T---检查多少次后关机,每次为60000毫秒,则1分钟。多少次,视你的UPS能撑多久而定。 原理:每分钟Ping指定IP,如路由器IP,如2次,则3分钟内还没有Ping通,则视为停电,自动关机。 请注意 请在源码中\ServerAutoShutDown\bin\Debug,将ServerAutoShutDown.exe复制出来,上传时忘记更新根目录那个文件了,那个是测试来的,关不了机

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;
namespace ServerAutoShutDown
{
    public partial class AutoShutDown : Servicebase
    {
        System.Timers.Timer t = new System.Timers.Timer(60000);
        int ptimes = 0;
        public AutoShutDown()
        {
            InitializeComponent();
        }

        protected override void onstart(string[] args)
        {
            t.Elapsed += new System.Timers.ElapsedEventHandler(PingTest);
            t.Enabled = true;
            t.AutoReset = true;
            t.Start();

        }
        private void PingTest(object source System.Timers.ElapsedEventArgs e)
        {
            try
            {
                INIFile ini = new INIFile(Application.StartupPath + “\\Config.ini“);
                Ping pingSender = new Ping();
                string strIP = ini.IniReadValue(“Config“ “IP“);
                PingOptions pingOption = new PingOptions();
                pingOption.DontFragment = true;

                string data = “SendMsg“;
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 120;
                PingReply reply = pingSender.Send(strIP timeout buffer);
                if (reply.Status == IPStatus.Success)
                {
                    //Ping通了
                    ptimes = 0;
                }
                else
                {
                    //Ping不通

                    OutPutDebug(“  Ping Timeout“);
                    ptimes++;
                    if (ptimes > int.Parse(ini.IniReadValue(“Config“ “T“)))
                    {
                        //4分钟后没有通,则自动关机
                       OutPutDebug (“  Shutdown“);
                         Process.Start(@“c:\windows\system32\shutdown.exe“ “-s -t 0 -f“);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                OutPutDebug(ex.Message);
            }

        }

        private void OutPutDebug(string msg)
        {

            System.IO.StreamWriter tw = new System.IO.StreamWriter(Application.StartupPath + “\\Shutdown.log“ true Encoding.GetEncoding(“gb2312“));
            tw.WriteLine(DateTime.Now.ToString() + msg);
            tw.Close();
        }
        protected override void onstop()
        {

        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      28672  2006-02-28 15:06  SRVINSTW.EXE

     文件       7680  2010-07-03 22:04  ServerAutoShutDown.exe

     文件         30  2010-07-03 22:05  Config.ini

     目录          0  2010-07-03 22:18  源码

     文件        944  2010-07-01 21:51  源码\ServerAutoShutDown.sln

    ..A..H.     17920  2010-07-03 22:09  源码\ServerAutoShutDown.suo

     文件       2645  2010-07-03 22:09  源码\ServerAutoShutDown\AutoShutDown.cs

     文件       1097  2010-07-01 21:51  源码\ServerAutoShutDown\AutoShutDown.Designer.cs

     文件       8192  2010-07-03 22:09  源码\ServerAutoShutDown\bin\Debug\ServerAutoShutDown.exe

     文件      24064  2010-07-03 22:09  源码\ServerAutoShutDown\bin\Debug\ServerAutoShutDown.pdb

     文件      14328  2010-07-03 20:19  源码\ServerAutoShutDown\bin\Debug\ServerAutoShutDown.vshost.exe

     文件        490  2007-07-21 01:33  源码\ServerAutoShutDown\bin\Debug\ServerAutoShutDown.vshost.exe.manifest

     文件       3729  2010-07-03 21:48  源码\ServerAutoShutDown\clsINI.cs

     文件        418  2010-07-03 22:04  源码\ServerAutoShutDown\obj\Debug\ServerAutoShutDown.csproj.FileListAbsolute.txt

     文件       8192  2010-07-03 22:09  源码\ServerAutoShutDown\obj\Debug\ServerAutoShutDown.exe

     文件      24064  2010-07-03 22:09  源码\ServerAutoShutDown\obj\Debug\ServerAutoShutDown.pdb

     文件        516  2010-07-01 21:51  源码\ServerAutoShutDown\Program.cs

     文件       1383  2010-07-01 23:01  源码\ServerAutoShutDown\Properties\AssemblyInfo.cs

     文件       2863  2010-07-03 21:47  源码\ServerAutoShutDown\ServerAutoShutDown.csproj

     目录          0  2010-07-01 21:51  源码\ServerAutoShutDown\obj\Debug\Refactor

     目录          0  2010-07-01 21:51  源码\ServerAutoShutDown\obj\Debug\TempPE

     目录          0  2010-07-01 23:18  源码\ServerAutoShutDown\bin\Debug

     目录          0  2010-07-03 22:04  源码\ServerAutoShutDown\obj\Debug

     目录          0  2010-07-01 21:51  源码\ServerAutoShutDown\bin

     目录          0  2010-07-01 21:51  源码\ServerAutoShutDown\obj

     目录          0  2010-07-01 23:01  源码\ServerAutoShutDown\Properties

     目录          0  2010-07-03 22:09  源码\ServerAutoShutDown

----------- ---------  ---------- -----  ----

               147227                    27



............此处省略0个文件信息

评论

共有 条评论