杀进程和取文件最近使用时间

unit uFTPclient;

interface

uses
  SysUtils,Windows,Tlhelp32;

const
   FILE_CREATE_TIME=0;    //文件建立时间
   FILE_MODIFY_TIME=1;    //修改时间
   FILE_ACCESS_TIME=2;    //最后访问时间

type
  TFileTimes   =   (ftLastAccess,   ftLastWrite,   ftCreation);

//文件是否正被使用
function IsFileInUse(FName:string):Boolean;
//提升权限
//杀服务程序进程,它会提示"拒绝访问".其实只要程序拥有Debug权限即可
//使用的时候先EnableDebugPrivilege提升权限,然后KillTask(ExeFileName: string)
function EnableDebugPrivilege: Boolean;
//查找进程  
function FindProcessId(ExeFileName: string):THandle;
//杀进程
function KillTask(ExeFileName: string): Integer;
//取文件最后使用时间
function GetFileLastAccessTime(sFileName:string;uFlag:byte):TDateTime;
//设置文件最后修改时间
Function SetFileLastWriteTime(FileName:string; DateTime: TDateTime): Integer;

implementation

Function SetFileLastWriteTime(FileName:string; DateTime: TDateTime): Integer;
var
  hFile                 : THandle;
  WriteTime, LocalTime  : TFILETIME;
  SystemTime            : TSystemTime;
begin
  result:= 0;
  try
    hFile := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
    if hFile <= 0 then
    begin
      result:= 1;
    end else
    begin
      DateTimeToSystemTime(DateTime, SystemTime);
      SystemTimeToFileTime(SystemTime, LocalTime);
      LocalFileTimeToFileTime(Localtime, WriteTime);

      If not SetFileTime(hFile, nil, nil, @WriteTime) then
      begin
        result:= 2;
      end;
    end;
  finally
    FileClose(hFile);
  end;
end;

function IsFileInUse(FName:string):Boolean;  
var  
  HFileRes:HFILE;  
begin
  Result:=False;
  if not FileExists(FName) then
    Exit;
  HFileRes:=CreateFile(PChar(FName),GENERIC_READ or GENERIC_WRITE,0,
    nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  Result:=(HFileRes=INVALID_HANDLE_VALUE);
  if not Result then
    CloseHandle(HFileRes);
end;

function EnableDebugPrivilege: Boolean;
  function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
  var
  TP: TOKEN_PRIVILEGES;
  Dummy: Cardinal;
  begin
  TP.PrivilegeCount := 1;
  LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
  if bEnable then
  TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
  else TP.Privileges[0].Attributes := 0;
  AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
  Result := GetLastError = ERROR_SUCCESS;
  end;
var
  hToken: Cardinal;
begin
  OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
  result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True);
  CloseHandle(hToken);
end;

function FindProcessId(ExeFileName: string):THandle;
var
  ContinueLoop:BOOL;
  FSnapshotHandle:THandle;
  FProcessEntry32:TProcessEntry32;
begin
  result:=0;
  FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
  ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
  while integer(ContinueLoop)<>0 do
  begin
    if UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName) then
    begin
      result:=FProcessEntry32.th32ProcessID;
      break;
    end;
    ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
  end;
  CloseHandle (FSnapshotHandle);
end;

function KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: boolean;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

  while Integer(ContinueLoop) <> 0 do
  begin
  if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
  UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
  UpperCase(ExeFileName))) then
  Result := Integer(TerminateProcess(
  OpenProcess(PROCESS_TERMINATE,
  BOOL(0),
  FProcessEntry32.th32ProcessID),
  0));
  ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

function GetFileLastAccessTime(sFileName:string;uFlag:byte):TDateTime;
var
  ffd:TWin32FindData;
  dft:DWord;
  lft:TFileTime;
  h:THandle;
begin
  h:=FindFirstFile(PChar(sFileName),ffd);
  if h<>INVALID_HANDLE_VALUE then
  begin
  case uFlag of
  FILE_CREATE_TIME:FileTimeToLocalFileTime(ffd.ftCreationTime,lft);
  FILE_MODIFY_TIME:FileTimeToLocalFileTime(ffd.ftLastWriteTime,lft);
  FILE_ACCESS_TIME:FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
  else
    FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
  end;
  FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo);
  Result:=FileDateToDateTime(dft);
  windows.FindClose(h);
  end
  else
  result:=0;
end;

end.

你可能感兴趣的:(文件)