win services 自动安装

1.设置程序权限,以“管理员身份运行” 转https://blog.csdn.net/scgh_fx/article/details/72639367

#如何以管理员身份运行程序

     在使用winform程序获取调用cmd命令提示符时,如果是win7以上的操作系统,会需要必须以管理员身份运行才会执行成功,否则无效果或提示错误。

     比如在通过winform程序执行cmd命令时,某些情况下如果不是以管理员身份运行,则会提示命令无效。

     或者通过winform程序执行Windows Service 服务时,也需要以管理员身份才能调用Service服务。

下面讲解一下如何使程序获取管理员权限来运行。

一: 在Visual Studio 中--解决方案资源管理器--右键项目名称--属性,找到“安全性”选项,

二:勾选“启用ClickOnce安全设置”,

三:这时,在项目下面会多出一个“app.manifest”的文件,选中它,并找到代码段<requestedExecutionLevel level="asInvoker" uiAccess="false" />,将其改为:<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,

打开:

将上图部分修改为:

四:改正后,不要急于重新编译生成,再次打开“属性--安全性”界面,

将“启用ClickOnce安全设置”前面的勾去掉后再编译运行。 不然程序会报错无法运行。

 

五:最后,保存修改,重新编译运行程序。

打开程序时,会提示“用户账户控制”来获取管理员权限运行,点击“是”则获取了管理员权限。

 

2.自动安装程序

   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;
            }
        }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值