Windows自动启动


经常我们需要设置一个程序运行以后 开机自动启动。代码实现如下

定时任务型

 

Windows自动启动

public static _TASK_STATE AutoStartup(string creator, string taskName, string path)
{
    try
    {
        //实例化任务对象
        TaskSchedulerClass scheduler = new TaskSchedulerClass();
        scheduler.Connect(null, null, null, null);//连接
        ITaskFolder folder = scheduler.GetFolder("//");
        //获取所有Task
        IRegisteredTaskCollection tasks = folder.GetTasks(1);
        bool isExists = false;
        for (int i = 1; i < tasks.Count; i++)
        {
            if (tasks[i].Name.Equals(taskName))
            {
                isExists = true;
                break;
            }
        }
        //如果已存在则先删除
        if (isExists)
        {
            folder.DeleteTask(taskName, 0);
        }

        //设置常规属性
        ITaskDefinition task = scheduler.NewTask(0);
        task.RegistrationInfo.Author = creator;//创建者
        task.RegistrationInfo.Description = "描述信息";//描述
        task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; //使用最高权限运行
                                                                        //设置触发器
        ILogonTrigger tt = (ILogonTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); //触发器里的开始任务,其他开始任务方式用的是其他接口
        tt.UserId = Environment.MachineName + "//" + creator; //特定用户
                                                              //设置操作
        IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
        action.Path = path;
        //其他设置
        task.Settings.ExecutionTimeLimit = "PT0S";
        task.Settings.DisallowStartIfOnBatteries = false;
        task.Settings.RunOnlyIfIdle = false;

        //注册任务
        IRegisteredTask regTask = folder.RegisterTaskDefinition(
        taskName, //计划任务名称
        task,
        (int)_TASK_CREATION.TASK_CREATE, //创建
        null, //user
        null, // password
        _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, //Principal.LogonType
        "");

        IRunningTask runTask = regTask.Run(null);
        return runTask.State;
    }
    catch (Exception)
    {
        return _TASK_STATE.TASK_STATE_UNKNOWN;
    }
}

View Code

 

 注册表型

Windows自动启动

 public static void AutoStart(bool isAuto = true, bool showinfo = true)
        {
            try
            {
                if (isAuto == true)
                {
                    RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
                    R_run.SetValue("应用名称", Application.ExecutablePath);
                    R_run.Close();
                    R_local.Close();
                }
                else
                {
                    RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
                    R_run.DeleteValue("应用名称", false);
                    R_run.Close();
                    R_local.Close();
                }
            }


             //if (showinfo)
            //      MessageBox.Show("您需要管理员权限修改", "提示");
            // Console.WriteLine("您需要管理员权限修改");
            catch (Exception ex)
            {
                string content = DateTime.Now.ToLocalTime() + " 0001_" + "您需要管理员权限修改" + "/n" + ex.StackTrace + "/r/n";
                LogHelper.logWrite(content);
            }

        }

View Code

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/288281.html

(0)
上一篇 2022年9月8日 15:00
下一篇 2022年9月8日 15:16

相关推荐

发表回复

登录后才能评论