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上鼠标右键启动项目->属性

 

自动安装时可以要项目以“管理员人份运行”,设置如下

 

 

此时在服务项目下

保存文件;此时不要生成文件,再次打开属性窗口,点击安全性

重新生成项目,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值