ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 设置手机开机就启动程序的类

设置手机开机就启动程序的类

原创 Linux操作系统 作者:freshairpeng 时间:2009-03-04 15:57:45 0 删除 编辑

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;

namespace AnotherTodayScreenItem.Utl
{
    ///


    /// 设置手机开机就启动程序的类
    ///

    class AutoRun
    {

        private static String myAppPath = GetAppPath();

        public static string GetAppPath()
        {
            //return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName); //GetName().CodeBase)
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //GetName().CodeBase)
        }

        ///


        /// 构造函数
        ///

        public AutoRun(string file_name)
        {
            string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);//Startup路径
            System.IO.File.Copy(myAppPath+file_name.Trim(), StartupPath + file_name+".exe", true);

            //获得文件的当前路径
            //string dir = Directory.GetCurrentDirectory();
            //获取可执行文件的全部路径
            string exeDir = myAppPath+file_name+".exe";

            //获取Run键
            RegistryKey key1 = Registry.LocalMachine;
            RegistryKey key2 = key1.CreateSubKey("SOFTWARE");
            RegistryKey key3 = key2.CreateSubKey("Microsoft");
            RegistryKey key4 = key3.CreateSubKey("Windows");
            RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
            RegistryKey key6 = key5.CreateSubKey("Run");
            //在Run键中写入一个新的键值
            key6.SetValue("myForm", exeDir);
            key6.Close();

            //如果要取消的话就将key6.SetValue("myForm",exeDir);改成
            //key6.SetValue("myForm",false);   
       
        }


        ///


        /// 设置应用程序开机自动运行
        ///

        /// 应用程序的文件名
        /// 是否自动运行,为false时,取消自动运行
        /// 设置不成功时抛出异常
        public static void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            string name = myAppPath + fileName.Trim() + ".exe";
            try
            {
                if (!System.IO.File.Exists(fileName))
                    throw new Exception("该文件不存在!");
                name = name.Substring(fileName.LastIndexOf("\\") + 1);
                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(name, fileName);
                else
                    reg.SetValue(name, false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }

        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14766526/viewspace-561384/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 发起GPRS拨号请求
请登录后发表评论 登录
全部评论

注册时间:2009-02-25

  • 博文量
    69
  • 访问量
    247531