Mega Code Archive

 
Categories / Delphi / System
 

How to create a registry entry in the autorun key

Title: How to create a registry entry in the autorun key // Add the application to the registry... procedure DoAppToRunOnce(RunName, AppName: string); var Reg: TRegistry; begin Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion\RunOnce', True); WriteString(RunName, AppName); CloseKey; Free; end; end; // Check if the application is in the registry... function IsAppInRunOnce(RunName: string): Boolean; var Reg: TRegistry; begin Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion\RunOnce', False); Result := ValueExists(RunName); CloseKey; Free; end; end; // Remove the application from the registry... procedure DelAppFromRunOnce(RunName: string); var Reg: TRegistry; begin Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion\RunOnce', True); if ValueExists(RunName) then DeleteValue(RunName); CloseKey; Free; end; end; Applications under the key "Run" will be executed each time the user logs on. // Add the application to the registry... procedure DoAppToRun(RunName, AppName: string); var Reg: TRegistry; begin Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True); WriteString(RunName, AppName); CloseKey; Free; end; end; // Check if the application is in the registry... function IsAppInRun(RunName: string): Boolean; var Reg: TRegistry; begin Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', False); Result := ValueExists(RunName); CloseKey; Free; end; end; // Remove the application from the registry... procedure DelAppFromRun(RunName: string); var Reg: TRegistry; begin Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True); if ValueExists(RunName) then DeleteValue(RunName); CloseKey; Free; end; end; Usage Examples: // Add app, DoAppToRun('Programm', 'C:\Programs\XYZ\Program.exe'); // Is app there ? if IsAppInRun('Programm') then... // Remove app, DelAppFromRun('Programm');