遇到个简单又非常变态 神经病 的问题, 有经验的帮忙看下。
unit XJH;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, Functions, StdCtrls;
type
TForm1 = class(TForm)
lbl2: TLabel;
lbl1: TLabel;
edt_chdPath: TEdit;
btn1: TButton;
btn2: TButton;
dlgOpen1: TOpenDialog;
procedure btn2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
if(dlgOpen1.Execute) then
begin
edt_chdPath.Text := dlgOpen1.FileName;
end;
end;
procedure TForm1.btn2Click(Sender: TObject);
var
comStr: string;
exeName: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
Tid: THandle;
begin
if (not FileExists(edt_chdPath.Text)) then // 文件不存在 直接跳出
Exit;
exeName := edt_chdPath.Text;
comStr := ' /Window /Pack';
FillChar(ProcessInfo, Sizeof(TProcessInformation), 0);
FillChar(StartupInfo, Sizeof(TStartupInfo), 0);
StartupInfo.cb := Sizeof(TStartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOW;
If CreateProcess(PWideChar(exeName), PWideChar(comStr), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
begin
end;
end;
end.
CreateProcess('直接写路径', '直接写命令行', nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo)
CHDIR(ExtractFilePath(exeName)); //加上这句
If CreateProcess(PWideChar(exeName), PWideChar(comStr), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
begin
end;
ini保存,由于未打开OpenDialog,当前路径还是在原路径,