1.如图所示:
解释: ---install.bat 安装bat
---InstallUtil.exe C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727目录下文件
---InstallUtilLib.dll C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727目录下文件
---***.ReminderService.exe 程序debug文件夹下的生成文件
---***.ReminderService.exe.config 程序debug文件夹下的生成文件
---start.bat 启动服务bat
---stop.bat 停止服务bat
---uninstall.bat 卸载bat
2.详解每个文件的内容(以Princess.ReminderService服务为例子)
---install.bat
@echo off
***.ReminderService.exe -install
pause
---start.bat
@echo off
Princess.ReminderService.exe -start
pause
---stop.bat
@echo off
***.ReminderService.exe -stop
pause
---uninstall.bat
@echo off
***.ReminderService.exe -remove
pause
***.ReminderService实例Program内容(并往系统里写入日志):

static class Program { ///// <summary> ///// The main entry point for the application. ///// </summary> //static void Main() //{ // ServiceBase[] ServicesToRun; // ServicesToRun = new ServiceBase[] // { // new ReminderService() // }; // ServiceBase.Run(ServicesToRun); //} static public int Main(string[] Args) { if (!EventLog.SourceExists("Reminder Service")) { EventLog.CreateEventSource("Reminder Service", "*** Email Reminder"); } EventLog log = new EventLog(); log.Source = "Reminder Service"; log.Log = "*** Email Reminder"; if (Args.Length == 0) { ServiceBase.Run(new ReminderService()); Console.WriteLine("Options:"); Console.WriteLine(" -install\tinstall \"Converter\" service"); Console.WriteLine(" -remove\tuninstall \"Converter\" service"); Console.WriteLine(" -start\t\tstart service"); Console.WriteLine(" -stop\t\tstop service"); Console.WriteLine(" -do\t\tdo some action"); Console.WriteLine(" check"); return 0; } // -install if (Args[0].Equals("-install")) { if (ServiceSetup(true)) { log.WriteEntry("*** Reminder Service installed successfully."); } } // -remove else if (Args[0].Equals("-remove")) { if (ServiceSetup(false)) { log.WriteEntry("*** Reminder Service removed successfully."); } else { log.WriteEntry("*** Reminder Service was removed unsuccessfully."); } } // -start else if (Args[0].Equals("-start")) { ServiceController controller = new ServiceController(ServiceInfo.SERVICE_NAME); try { if (controller.Status == ServiceControllerStatus.Stopped) { controller.Start(Args); } else { log.WriteEntry("Exception: *** Reminder Service is not stoped!"); } } catch (Exception ex) { log.WriteEntry(ex.Message, EventLogEntryType.Error); } controller.Dispose(); } // -stop else if (Args[0].Equals("-stop")) { ServiceController controller = new ServiceController(ServiceInfo.SERVICE_NAME); try { if (controller.Status == ServiceControllerStatus.Running) { controller.Stop(); } else { log.WriteEntry("Exception: *** Reminder Service is not running."); } } catch (Exception ex) { log.WriteEntry(ex.Message, EventLogEntryType.Error); } controller.Dispose(); } return 0; } static public bool ServiceSetup(bool blInstall) { string fullpath = Process.GetCurrentProcess().MainModule.FileName; string path = AppDomain.CurrentDomain.BaseDirectory; string filename = fullpath.Substring(fullpath.LastIndexOf('\\') + 1); if (!File.Exists(path + "InstallUtil.exe") || !File.Exists(path + "InstallUtilLib.dll")) { EventLog log = new EventLog(); log.Source = "ReminderService"; log.WriteEntry("InstallUtil.exe or InstallUtilLib.dll is not existed!"); return false; } Process proc = new Process(); proc.StartInfo.FileName = path + "InstallUtil.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.Arguments = "/" + (blInstall ? "" : "un") + "install /LogFile= /LogToConsole=false " + filename; proc.Start(); proc.WaitForExit(); string output = proc.StandardOutput.ReadToEnd(); if (output.IndexOf("exception") > 0) return false; return true; } }