• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#执行bat文件批处理-实现应用程序开机启动功能

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近在做一个项目(平台 .net 4.0 winform)的时候,客户要求软件能提供开机启动的设置选项

开始的时候,实现方法如下:

public class Boot
    {
        //写入注册表
        public static void bootFromBoot(string ExeName, string ExePath)
        {
            RegistryKey rKey = Registry.LocalMachine;
            RegistryKey autoRun = rKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            
            try
            {
                autoRun.SetValue(ExeName, ExePath);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //删除注册表
        public static void deleteFromBoot(string ExeName, string ExePath)
        {
            RegistryKey rKey = Registry.LocalMachine;
            RegistryKey autoRun = rKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

            try
            {
                //autoRun.DeleteValue(ExePath);
                if (autoRun.GetValue(ExeName) != null)
                    autoRun.DeleteValue(ExeName);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

但是有个问题,应用程序没有管理员权限的时候,写入注册表会失败,在winform的项目属性里面打开了获取管理员权限

这回成功了,可以应用程序在win10系统上运行的时候,无法将桌面快捷方式拖进应用程序,报错,

找到原因是:取消管理员权限的时候,在win10上就能正常运行了,但是开机启动写入注册表就不行了,

脑袋瓜还是好使的(感谢上帝赐给的智慧),想到用windows的批处理文件进行注册表写入

#region bat批处理

        public static void CreateRegDeleteBat(string file_path, string key)
        {
            if (System.IO.File.Exists(file_path))
            {
                System.IO.File.Delete(file_path);
            }

            string bat = "@echo off" + "\r\n";
            bat += "cacls.exe \"%SystemDrive%\\System Volume Information\" >nul 2>nul" + "\r\n";
            bat += "if %errorlevel%==0 goto Admin" + "\r\n";
            bat += "echo request Administrator" + "\r\n";
            bat += "if exist \"%temp%\\getadmin.vbs\" del /f /q \"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "echo Set RequestUAC = CreateObject^(\"Shell.Application\"^)>\"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "echo RequestUAC.ShellExecute \"%~s0\",\"\",\"\",\"runas\",1 >>\"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "echo WScript.Quit >>\"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "\"%temp%\\getadmin.vbs\" /f" + "\r\n";
            bat += "if exist \"%temp%\\getadmin.vbs\" del /f /q \"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "exit" + "\r\n";

            bat += ":Admin" + "\r\n";
            bat += "echo delete reg" + "\r\n";
            bat += "reg delete \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v " + key + " /f" + "\r\n";

            //bat += "PAUSE";

            System.IO.File.WriteAllText(file_path, bat, Encoding.Default);
        }

        public static void CreateRegAddBat(string file_path, string key, string exeFullPath)
        {
            if (System.IO.File.Exists(file_path))
            {
                System.IO.File.Delete(file_path);
            }

            string bat = "@echo off" + "\r\n";
            bat += "cacls.exe \"%SystemDrive%\\System Volume Information\" >nul 2>nul" + "\r\n";
            bat += "if %errorlevel%==0 goto Admin" + "\r\n";
            bat += "echo request Administrator" + "\r\n";
            bat += "if exist \"%temp%\\getadmin.vbs\" del /f /q \"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "echo Set RequestUAC = CreateObject^(\"Shell.Application\"^)>\"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "echo RequestUAC.ShellExecute \"%~s0\",\"\",\"\",\"runas\",1 >>\"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "echo WScript.Quit >>\"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "\"%temp%\\getadmin.vbs\" /f" + "\r\n";
            bat += "if exist \"%temp%\\getadmin.vbs\" del /f /q \"%temp%\\getadmin.vbs\"" + "\r\n";
            bat += "exit" + "\r\n";

            bat += ":Admin" + "\r\n";
            bat += "echo add reg" + "\r\n";
            bat += "reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v " + key + " /d " + "\"" + exeFullPath + "\"" + " /f" + "\r\n";

            //bat += "PAUSE";

            System.IO.File.WriteAllText(file_path, bat, Encoding.Default);
        }

        public static void RunBATfile(string file_path, string file_name)
        {
            Process pro = new Process();
            pro.StartInfo.FileName = file_name;
            pro.StartInfo.WorkingDirectory = file_path;
            pro.StartInfo.CreateNoWindow = true;
            pro.Start();
            pro.WaitForExit();
        }

        #endregion

 

使用的时候这样:

private void reg_add()
        {
            string exe_path = Application.StartupPath;
            string file_name = "add_reg.bat";
            string full_path = exe_path + "\\" + file_name;

            string IconSyncPath = Application.ExecutablePath;

            FileHelper.CreateRegAddBat(full_path, "IconSync", IconSyncPath);
            FileHelper.RunBATfile(exe_path, file_name);
        }

        private void reg_delete()
        {
            string exe_path = Application.StartupPath;
            string file_name = "delete_reg.bat";
            string full_path = exe_path + "\\" + file_name;

            FileHelper.CreateRegDeleteBat(full_path, "IconSync");
            FileHelper.RunBATfile(exe_path, file_name);
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++的可变参数模板函数发布时间:2022-07-13
下一篇:
.net全栈开发-c#面向对象与工控自动化分拣上位机发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap