設定手機開機就啟動程式的類
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [ubuntu]設定開機自啟動程式的方法Ubuntu
- lubuntu設定開機啟動Ubuntu
- 給 .NET 程式加個「設定開機啟動」
- linux設定開機啟動Linux
- centos設定Apache開機啟動CentOSApache
- win10開機啟動項怎麼設定 win10設定開機啟動項的方法Win10
- centos 服務開機啟動設定CentOS
- 設定開機自啟動nginx和httpdNginxhttpd
- ubuntu 設定Path 開機啟動指令碼Ubuntu指令碼
- AIX 開機自啟動指令碼設定AI指令碼
- linux 中oracle開機啟動設定LinuxOracle
- win10開機啟動項怎麼設定_如何設定win10系統開機啟動項Win10
- postgresql隨機啟動設定SQL隨機
- 動態軟體如何設定雲手機的程式的?
- centos7.5 設定Mysql開機自啟動CentOSMySql
- systemctl 命令設定開機自啟動失敗
- 電腦開機啟動項在哪裡設定
- ubuntu14 設定開機啟動指令碼Ubuntu指令碼
- 設定Oracle資料庫開機自啟動Oracle資料庫
- Winform設定開機啟動-操作登錄檔ORM
- windows下redis設定redis開機自啟動WindowsRedis
- win10開機啟動項怎麼設定 win10新增開機啟動項Win10
- 設定msyqlphp-fpm開機自動啟動指令碼PHP指令碼
- win10開機啟動項如何管理_win10開機啟動項怎麼設定Win10
- windows10開機啟動項在哪裡_windows10開機啟動項怎麼設定Windows
- win10開機啟動項怎麼設定 win10開機啟動項在哪裡Win10
- win10怎麼新增開機啟動項 win10開機啟動項如何設定Win10
- 巧妙地利用程式分離來設定程式開機自動啟動!(附原始碼)原始碼
- 如何設定win10軟體開機自動啟動 win10軟體開機自動啟動的方法Win10
- JavaFx 建立快捷方式及設定開機啟動Java
- ubuntu-18.04 設定開機啟動指令碼Ubuntu指令碼
- TongWeb在Linux下設定開機自啟動WebLinux
- Linux平臺下oracle設定開機啟動LinuxOracle
- dell r910開機光碟啟動設定
- Mongodb 啟動關閉指令碼並設定開機自動啟動MongodbMongoDB指令碼
- frp設定開機自啟FRP
- win10開機啟動項怎麼設定順序啟動的方法Win10
- win10自定義開機啟動項的步驟_win10怎樣設定開機啟動項Win10