系统相关
首页 > 系统相关> > 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

 

 注册表型

 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

 

标签:RegistryKey,task,run,启动,Windows,TASK,自动,null,local
来源: https://www.cnblogs.com/stweily/p/16668247.html