在網(wǎng)上找的代碼都是直接設(shè)置的添忘,放到 Form1_Load
中程序每次啟動(dòng)就注冊感覺怪怪的
就想辦法如何在注冊前先判斷程序有沒有注冊過散庶,通過F12找到了GetValueNames
方法蕉堰。
private void autorun()
{
//獲取程序路徑
string execPath = Application.ExecutablePath;
bool isexc = false;
try
{
RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
//設(shè)置自啟的程序叫獲取目錄下的程序名字
string[] ar = RKey.GetValueNames();
foreach (string st in ar)
{
if(st.Equals("test"))
{
isexc = true;
}
}
if (!isexc)
{
//設(shè)置自啟的程序叫test
RKey.SetValue("test", execPath);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}