I've written an application that should be run while starting windows.
I used this code to use a checkbox to decide whether it runs while startup or not:
// d('randomString'); -> this is a function which adds text to a memo for debugging purposes
// GetDosOutput is a function to run cmd commands and get the output of them piped in a memo
function GetRegistryValue(KeyName: string): string;
var
Registry: TRegistry;
begin
Registry := TRegistry.Create(KEY_READ);
try
Registry.RootKey := HKEY_CURRENT_USER;
// False weil kein Eintrag erzeugt werden soll, sofern er nicht vorhanden ist.
Registry.OpenKey(KeyName, False);
result := Registry.ReadString('SomeRandomAppIWantToRun');
finally
Registry.Free;
end;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
var
reg: TRegistry;
begin
if CheckBox1.Checked = true then
begin
with TRegistry.Create do
try
RootKey := HKEY_CURRENT_USER;
OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', False);
if ValueExists('SomeRandomAppIWantToRun') then
begin
d('Wert existiert');
if lowercase(Application.ExeName)
= lowercase
(GetRegistryValue('\Software\Microsoft\Windows\CurrentVersion\Run'))
then
begin
d('Autostart entry exists and is correct.');
end
else
begin
d('wrong value exists... will be deleted and recreated!');
GetDosOutput
('reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SomeRandomAppIWantToRun /f');
GetDosOutput
('REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v SomeRandomAppIWantToRun /t REG_SZ /d C:\temp\SomeRandomAppIWantToRun.exe');
end;
end
else
begin
d('Autostart entry doesnt exists and will be created now.');
GetDosOutput
('REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v SomeRandomAppIWantToRun /t REG_SZ /d C:\temp\SomeRandomAppIWantToRun.exe');
end;
except
showmessage
(d('Exception in Registry - stuff isnt working'));
end;
end
else
begin
with TRegistry.Create do
try
RootKey := HKEY_CURRENT_USER;
OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', False);
if ValueExists('SomeRandomAppIWantToRun') then
begin
d('Wert existiert');
if lowercase(Application.ExeName)
= lowercase
(GetRegistryValue('\Software\Microsoft\Windows\CurrentVersion\Run'))
then
begin
d('correct Autostart entry will be deleted!');
GetDosOutput
('reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SomeRandomAppIWantToRun /f');
end
else
begin
d('wrong startup value... will be deleted and not recreated!');
GetDosOutput
('reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v SomeRandomAppIWantToRun /f');
end;
end
else
begin
showmessage('Autostart entry doesnt exist and thats fine.');
end;
except
showmessage
(d('something didnt work well....'));
end;
end;
end;
It successfulls starts while booting up my computer, but.. it seems to be working in C:\windows\system32 and I dont know why.. I placed the application (.exe file) in C:\temp and it is supposed to do things in C:\temp like renaming folders and deleting files and so on, but for this it needs to run in C:\temp so it can easily for example run a batch file located in there which itself needs to think its running in C:\temp.
When I create a shortcut in %appdata%\Microsoft\Windows\Start Menu\Programs\Startup it works fine, but I personally dont want to create a shortcut in some directory, but like to do it in the registry