天天看點

C# WinForm 開機自動運作

第一種方法:

C# WinForm 開機自動運作
C# WinForm 開機自動運作

代碼

using Microsoft.Win32;

private void Form1_Load(object sender, EventArgs e)

{

    //擷取程式執行路徑..

    string starupPath = Application.ExecutablePath;

    //class Micosoft.Win32.RegistryKey. 表示Window系統資料庫中項級節點,此類是系統資料庫裝.

    RegistryKey loca = Registry.LocalMachine;

    RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

    try

    {

       //SetValue:存儲值的名稱

 run.SetValue("WinForm",starupPath);

 MessageBox.Show(" 系統資料庫添加成功!!",""提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

        loca.Close();

    }

    catch(Exception ee)

        MessageBox.Show(ee.Message.ToString(),"提 示",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

第二種方法:

添加到系統資料庫裡直接用代碼寫到系統資料庫裡,也可以手動添加.

E:D:\\tractor.exe//可以是你的程式名和 完整路徑就OK了.

也可以手動拖到啟動裡面....

C# WinForm 開機自動運作
C# WinForm 開機自動運作

RegistryKey hklm = Application.LocalMachine;

 RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Current\Version\Run");

 try

 {

    run.SetValue("tractor.exe","D:\\tractor.exe");

    MessageBox.Show("系統資料庫添加成功!!","提示",MessageBoxButton.OK, MessageBoxIcon.Information);

    hklm.Close();

 }

 catch(Exception ee)

    MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButton.OK, MessageBoxIcon.Error);

  }

繼續閱讀