用delphi怎樣來實現程序的自動啟動功能﹖

解决方案 »

  1.   

    Var
        RegF:TRegistry;
    begin
        RegF:=TRegistry.Create;
        RegF.RootKey:=HKEY_LOCAL_MACHINE;
        RegF.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run',True);
        RegF.WriteString('jkxt',application.ExeName);   //加入启动
        RegF.Free;
    end;
      

  2.   

    //==============================================================================
    //操作系统启动时自动运行指定程序************************************************
    //==============================================================================
    procedure SetAutoRun(Application, EntirePath: string; Ensure: boolean);
    var AutoRunReg: TRegistry;
    begin
      AutoRunReg := TRegistry.Create;
      AutoRunReg.RootKey := HKEY_LOCAL_MACHINE;
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      try
        AutoRunReg.OpenKey(GetRegAutoRunKey, True);
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if AutoRunReg.ValueExists(Application) then AutoRunReg.DeleteValue(Application);
        if Ensure then AutoRunReg.WriteString(Application, EntirePath);
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        AutoRunReg.CloseKey;
      finally
        AutoRunReg.Free;
      end;
    end;
      

  3.   

    //==============================================================================
    //获得注册表中操作系统启动时自动运行主键值**************************************
    //==============================================================================
    function GetRegAutoRunKey: string;
    begin
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      //获得Windows版本*************************************************************
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      case Win32Platform of
        VER_PLATFORM_WIN32_WINDOWS: Result := '\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\';
        VER_PLATFORM_WIN32_NT:      case Win32MajorVersion of
                                      4: Result := '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Run\';
                                      5: Result := '\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\';
                                    end;
      end;
    end;