Win7 开机启动

1.注册表里面写代码,设置程序以开机启动; 但这样会需要管理员权限,添加程序以管理员权限启动后,又无法直接进入到软件启动界面,UAC控制

 

代码一:

/// <summary>
        /// 设置应用程序开机自动运行
        /// </summary>
        /// <param name="fileName">应用程序的文件名</param>
        /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
        /// <exception cref="System.Exception">设置不成功时抛出异常</exception>
        public static void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

                if (isAutoRun)
                    reg.SetValue("WITDASHELL", fileName);
                else
                    reg.SetValue("WITDASHELL", false);
                reg.Close();
            }
            catch (Exception ex)
            {
                //屏蔽没有权限导致的报错信息
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }
     }
View Code

代码二:

http://www.cnblogs.com/MaxIE/archive/2013/04/17/3027229.html       (启动时获取管理员权限)

 

2.使用Window 服务

本来这个是应该没有问题的,但是工作环境使用的是嵌入式WES, 不知道哪个地方被自己删除了,导致所有服务都无法在界面上面查看,所以自己写了一堆可以在本机运行的代码后,部署在测试机的时候傻眼了

又不想立刻重新安装测试机的操作系统环境;

 

3.使用任务计划

代码一:

#if  !DEBUG
            //开机启动程序
            DotNet.Utilities.Helper.TaskHelper th = new DotNet.Utilities.Helper.TaskHelper();
            th.CreateTaskScheduler("author", "开机启动软件", "StartAPP", Path.Combine(Directory.GetCurrentDirectory(), "WITDA.Shell.exe"));
#endif
View Code

 

 

代码二:

 public class TaskHelper
    {
        /// <summary>
        /// delete task
        /// </summary>
        /// <param name="taskName"></param>
        private static void DeleteTask(string taskName)
        {
            TaskSchedulerClass ts = new TaskSchedulerClass();
            ts.Connect(null, null, null, null);
            ITaskFolder folder = ts.GetFolder("\\");
            folder.DeleteTask(taskName, 0);
        }

        /// <summary>
        /// get all tasks
        /// </summary>
        public static IRegisteredTaskCollection GetAllTasks()
        {
            TaskSchedulerClass ts = new TaskSchedulerClass();
            ts.Connect(null, null, null, null);
            ITaskFolder folder = ts.GetFolder("\\");
            IRegisteredTaskCollection tasks_exists = folder.GetTasks(1);
            return tasks_exists;
        }

        /// <summary>
        /// check task isexists
        /// </summary>
        /// <param name="taskName"></param>
        /// <returns></returns>
        public static bool IsExists(string taskName)
        {
            var isExists = false;
            IRegisteredTaskCollection tasks_exists = GetAllTasks();
            for (int i = 1; i <= tasks_exists.Count; i++)
            {
                IRegisteredTask t = tasks_exists[i];
                if (t.Name.Equals(taskName))
                {
                    isExists = true;
                    break;
                }
            }
            return isExists;
        }

        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="creator">创建任务的人员名称</param>
        /// <param name="taskName">任务名称</param>
        /// <param name="path">执行程序的路径</param>
        public void CreateTaskScheduler(string creator, string Description, string taskName, string path)
        {
            try
            {
                if (IsExists(taskName))
                {
                    return;
                }

                //new scheduler
                TaskSchedulerClass scheduler = new TaskSchedulerClass();
                //pc-name/ip,username,domain,password
                scheduler.Connect(null, null, null, null);
                //get scheduler folder
                ITaskFolder folder = scheduler.GetFolder("\\");


                //set base attr 
                ITaskDefinition task = scheduler.NewTask(0);
                task.RegistrationInfo.Author = creator;//creator
                task.RegistrationInfo.Description = Description;//description

                task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;

                //set trigger  (IDailyTrigger ITimeTrigger)
                task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);

                //set action
                IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
                action.Path = path;

                task.Settings.RunOnlyIfIdle = true;
                task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时停止任务吗? PTOS 不开启超时
                task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行
                task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行
                task.Settings.Priority = 0;

                IRegisteredTask regTask = folder.RegisterTaskDefinition(taskName, task,
                                                                    (int)_TASK_CREATION.TASK_CREATE, null, //user
                                                                    null, // password
                                                                    _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
                                                                    "");
            }
            catch (Exception ex)
            {

            }
        }
}
View Code

 

总结:

自己现在状态特别差,不是一天两天不想做事情了,自己要做好准备,迎接变化;每天打游戏不是你这么大的人应该做的 事情;

已经连续玩了一个月,不似caiguai

posted @ 2016-06-07 15:40  maanshancss  阅读(645)  评论(0编辑  收藏  举报