我撰寫了一個應在啟動 Windows 時運行的應用程式。
我使用此代碼使用復選框來決定它是否在啟動時運行:
// 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;
它在啟動我的計算機時成功啟動,但是......它似乎在 C:\windows\system32 中作業,我不知道為什么......我將應用程式(.exe 檔案)放在 C:\temp 中,它應該在 C:\temp 中做一些事情,比如重命名檔案夾和洗掉檔案等等,但為此它需要在 C:\temp 中運行,以便它可以輕松地例如運行位于那里的批處理檔案,它本身需要認為它正在運行C:\溫度。
當我在 %appdata%\Microsoft\Windows\Start Menu\Programs\Startup 中創建快捷方式時它作業正常,但我個人不想在某些目錄中創建快捷方式,但喜歡在注冊表中進行
uj5u.com熱心網友回復:
它在啟動我的計算機時成功啟動,但是.. 它似乎在 C:\windows\system32 中作業,我不知道為什么..
這是正常行為。當 Windows 從Run注冊表項運行應用程式時,它們會繼承 Shell 的作業目錄,也就是 System32 檔案夾。
您的程式不應依賴于在運行時作為任何特定值的作業目錄。如果要使用相對于 EXE 當前位置的檔案路徑,則應在運行時使用Application.ExeName或檢索 EXE 的完整路徑ParamStr(0),然后使用ExtractFilePath()去除檔案名。然后,您可以根據需要使用生成的字串創建其他檔案的路徑。
如果您在程式運行時絕對需要依賴作業目錄作為特定值,請參閱使用注冊表啟動程式,并更改當前作業目錄?解決方法。
當我在 %appdata%\Microsoft\Windows\Start Menu\Programs\Startup 中創建快捷方式時,它作業正常
那是因為快捷方式有自己的作業目錄。默認情況下,它與快捷方式的目標檔案夾相同。但是,如果您要進入該快捷方式的屬性并將其Start in欄位設定為不同的檔案夾,您會看到您的程式行為例外,與從Run注冊表項啟動時的行為方式相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/349265.html
標籤:视窗 德尔福 VCL 自动开启 delphi-10.4-悉尼
上一篇:mitmdump錯誤:windows中select()問題中的檔案描述符過多
下一篇:游戲全屏時保持chrome打開
