using Microsoft.Win32;//Registry的引用空间.

  1. private void FrmTiming_Load(object sender, EventArgs e)
  2. {
  3. AutoRun();
  4. }


    /// <summary>
    /// windows启动时,自动运行
    /// </summary>
    private void AutoRun()
  5. {
    try
  6. {
    string path = Application.ExecutablePath;
    if (!System.IO.File.Exists(path))
    return;
    string myName = path.Substring(path.LastIndexOf("""") + 1);
  7. RegistryKey myReg = Registry.LocalMachine.OpenSubKey("SOFTWARE""Microsoft""Windows""CurrentVersion""Run", true);
    if (myReg == null)
  8. myReg = Registry.LocalMachine.CreateSubKey("SOFTWARE""Microsoft""Windows""CurrentVersion""Run");
  9. myReg.SetValue(myName, path);
  10. }
    catch
  11. {
  12. Application.Exit();
  13. }

  14. }

 

版权声明:本文为wf5360308原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wf5360308/articles/1274771.html