新建一个WINDOWS服务,拖放一个组件下的TIMER,然后写代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;namespace PingService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            timer1.Interval = 10*60*1000;  //10分钟ping一次
            timer1.Start();
        }        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            ping("10.0.1.1");
        }        public static bool ping(string remoteHost)
        {
            bool Flag = false;
            Process proc = new Process();            try
            {
                proc.StartInfo.FileName="cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = @"ping -n 1 " + remoteHost;                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (proc.HasExited == false)
                {
                    proc.WaitForExit(500);
                }
                string pingResult = proc.StandardOutput.ReadToEnd();
                
                if (pingResult.IndexOf("(0% loss)") != -1)
                {
                    Flag = true;
                }
                proc.StandardOutput.Close() ;            }
            catch (Exception ex)
            {
            }
            finally
            {
                try
                {
                    proc.Close();
                    proc.Dispose();
                }
                catch
                {
                }
            }
            return Flag;        }
    }
}然后编译,然后用INSTALLUTIL安装
\bin\Debug\PingService.exe但是安装成功.在服务管理器里看不到,我安装的时候设置了LOGTOCONSOLE为TRUE