設定手機開機就啟動程式的類

freshairpeng發表於2009-03-04

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章