WindowsService 的安装与卸载
.bat的文件=>鼠标右键创建计事本,修改文件格式 (一般后缀改为.bat 确定即可)
1,安装文件 install.bat
@echo.请稍等,EohiQuartzService服务安装启动中............
@echo off
@title 安装windows服务:EohiQuartzService
@sc create EohiQuartzService binPath= "%~dp0\EohiQuartzService.exe" //服务启动文件的地址,安装文件和启动文件在同一目录 EohiQuartzService 名称要和启动文件一至 = 号后有一个空格 win10可以,若没有空格win7会报错
@sc config EohiQuartzService start= auto //启动方式为自动
@sc start EohiQuartzService //安装后就启动服务
@echo.EohiQuartzService启动完毕
pause
2.卸载文件 delete.bat
@echo.服务EohiQuartzService卸载中..........
@echo off
@sc stop EohiQuartzService
@sc delete EohiQuartzService //与后台服务名称一至
@echo off
@echo.EohiQuartzService卸载完毕
@pause
1.此后在同一目录下以“管理员身份运行”即可安装/卸载服务
2.服务与别的项目整合时,项目启动时若无此服务自动安装 (vs)
private Process proc = null;
/// <summary>
/// 安装服务
/// </summary>
private void InsertBat()
{
try
{
#region 此代码会提示未加载程序集。。。。。。
//string serviceFilePath = Environment.CurrentDirectory + "\\Install.bat";
//using (AssemblyInstaller installer = new AssemblyInstaller())
//{
// installer.UseNewContext = true;
// installer.Path = serviceFilePath;
// IDictionary savedState = new Hashtable();
// installer.Install(savedState);
// installer.Commit(savedState);
//}
#endregion
#region 测试安装
string str = System.Windows.Forms.Application.StartupPath + "\\Install.bat"; //文件地址(一般与启动文件同一目录)
string strDirPath = System.IO.Path.GetDirectoryName(str);
string strFilePath = System.IO.Path.GetFileName(str);
string targetDir = string.Format(strDirPath);//this is where mybatch.bat lies
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = strFilePath;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
//proc.WaitForExit();
#endregion
MessageBoxTimeOut messageBoxTimeOut = new MessageBoxTimeOut();
messageBoxTimeOut.ShowMessageBoxTimeout("执行成功!!!");
butqdfw.Enabled = false;
buttzfw.Enabled = true;
}
catch (Exception ex)
{
MessageBoxTimeOut messageBoxTimeOut = new MessageBoxTimeOut();
messageBoxTimeOut.ShowMessageBoxTimeout(ex.Message);
butqdfw.Enabled = false;
buttzfw.Enabled = true;
throw;
}
}
2.1 vs上鼠标右键启动项目->属性
自动安装时可以要项目以“管理员人份运行”,设置如下
此时在服务项目下
保存文件;此时不要生成文件,再次打开属性窗口,点击安全性
重新生成项目,