Checking if file is in useA few days ago I was asked how to tell if a given file is already being used by another application. Finding out if a file, given its name, is in use (open), is pretty simple. The process consists in trying to open the file for Exclusive Read/Write access. If the file is already in use, it will be locked (by the calling process) for exclusive access, and the call will fail. Please note that some application do not lock the file when using it. One clear example of this is NOTEPAD. If you open a TXT file in Notepad, the file will not be locked, so the function below will report the file as not being in use.The function below, IsFileInUse will return true if the file is locked for exclusive access. As it uses CreateFile, it would also fail if the file doesn't exists. In my opinion, a file that doesn't exist is a file that is not in use. That's why I added the FileExists call in the function. Anyway, here's the function: 
  
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 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
   Result := (HFileRes = INVALID_HANDLE_VALUE);
   if not Result then
   CloseHandle(HFileRes);
end;
   
NOTE: The function will return false if the specified file doesn't exist, meaning that it is not in use and can be used for something else. 

解决方案 »

  1.   

    Sending Files to the Recycle Bin You can easily delete files sending them to the Recycle Bin, rather them permanently deleting them. This is achieve through the use of the SHFileOperation API call (a VERY usefull call!) and the flag FOF_ALLOWUNDO. The routing below uses this call to send files to the Recycle Bin. Note that you must pass a valid filename, with full path and drive specs.Just one more thing: test this routine on your system with wasteable files before deleting anything important with it! uses
       ShellApi;function DeleteFileWithUndo(sFileName : string ) : boolean;
    var
      fos : TSHFileOpStruct;
    begin
      FillChar( fos, SizeOf( fos ), 0 );
      with fos do
      begin
        Wnd := application.handle;
        wFunc  := FO_DELETE;
        pFrom  := PChar( sFileName );
        fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT;
      end;
      Result := ( 0 = ShFileOperation( fos ) );
    end;
      

  2.   

    So your new form has 40 edit controls, and now you're looking for a quick and easy way to clear all of them without having to refer to them one by one. How about using a function like this: 
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i : integer;
    begin
      for i := 0 to ComponentCount-1 do
        begin
          if( Components[ i ] is TEdit )then
          begin
            (Components[ i ] as TEdit).Text := '';
          end;
      end;
    end; 
      

  3.   

    If you're still using good old INI files in your 32bit Windows programs, well, it's time to switch! The new Windows Registry is faster and space conscious than old INI files.Following two functions and step by step instructions will take some of the mystery out of using the registry for simple purposes:function LoadStringFromRegistry(
      sKey, sItem, sDefVal : string )
        : string;
    var
      reg : TRegIniFile;
    begin
      reg := TRegIniFile.Create( sKey );
      Result := reg.ReadString(
                  '', sItem, sDefVal );
      reg.Free;
    end;procedure SaveStringToRegistry(  sKey, sItem, sVal : string );
    var
      reg : TRegIniFile;
    begin
      reg := TRegIniFile.Create( sKey );
      reg.WriteString(
        '', sItem, sVal + #0 );
      reg.Free;
    end;
     
      

  4.   

    oh my god
    all of english
    我晕了,我晕了
      

  5.   

    oh my god
    all of english
    我晕了,我晕了
      

  6.   

    How to Create Shortcuts   To Create shortcuts in Delphi you have to use the com engine. Fortunately this is not too difficult. The following code shows you how:var
      slTemp: IShellLink;
      pfTemp: IPersistFile;
      sTarget: String;  { This is the path and name of the file that the   
                          shortcut is to point too }
      sIconFile : String; { This is the path and name of the file that contains
                            the icon (it can be an .exe or .ico file) }
      iIconIndex : Integer { The index of which icon to use in the specified file }
      sName : String  { The name for the shortcut including the path}begin
      // initialize the COM engine
      CoInitialize(nil);
      try
        // create an uninitialized IShellLink (a shortcut)
        if CoCreateInstance(CLSID_ShellLink,
                            nil, 
                            CLSCTX_INPROC_SERVER,
                            IShellLink, slTemp) = S_OK then
        begin
          slTemp.SetPath(PChar(sTarget));
          slTemp.SetIconLocation(PChar(sIconFile), iIconIndex);
        //Get an IPersistFile interface (needed to specify the name of the Shortcut)
          if slTemp.QueryInterface(IPersistFile, pfTemp) = S_OK then
            if pfTemp.Save(StringToOleStr(sName), True) <> S_OK then
              ShowMessage('could not save the shortcut');
        // do not Release slTemp, it is done automatically by Delphi
        end;
      // deinitialize COM
      finally
        CoUninitialize;
      end;
    end;
      

  7.   

    Redirecting DOS App Output  D2 D3 D4  
    A function to execute a DOS or Win32 consoloe mode application and wait for it to close before continuing. Input for the app can be directed from a file, and the output will be redirected to a file.  
    uses
      Controls, Windows, SysUtils, Forms;{---------------------------CreateDOSProcessRedirected--------------------------
     Description    : executes a (DOS!) app defined in the CommandLine parameter
                      redirected to take input from InputFile (optional) and give
                      output to OutputFile
     Result         : True on success
     Parameters     : CommandLine : the command line for app, including full path
                      InputFile   : the ascii file where from the app takes input,
                                     empty if no input needed/required.
                      OutputFile  : the ascii file to which the output is redirected
                      ErrMsg      : additional error message string. Can be empty
     Error checking : YES
     Target         : Delphi 2, 3, 4
     Author         : Theodoros Bebekis, email [email protected]
     Notes          :
     Example call   : CreateDOSProcessRedirected('C:\MyDOSApp.exe',
                                                 'C:\InputPut.txt',
                                                 'C:\OutPut.txt',
                                                 'Please, record this message')
    -------------------------------------------------------------------------------}
    function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile,
       ErrMsg :string): boolean;
    const
      ROUTINE_ID = '[function: CreateDOSProcessRedirected]';
    var
      OldCursor     : TCursor;
      pCommandLine  : array[0..MAX_PATH] of char;
      pInputFile,
      pOutPutFile   : array[0..MAX_PATH] of char;
      StartupInfo   : TStartupInfo;
      ProcessInfo   : TProcessInformation;
      SecAtrrs      : TSecurityAttributes;
      hAppProcess,
      hAppThread,
      hInputFile,
      hOutputFile   : THandle;
    begin
      Result := FALSE;  { check for InputFile existence }
      if (InputFile <> '') and (not FileExists(InputFile)) then
        raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
           'Input file * %s *' + #10 +
           'does not exist' + #10 + #10 +
           ErrMsg, [InputFile]);  hAppProcess := 0;
      hAppThread := 0;
      hInputFile := 0;
      hOutputFile := 0;  { save the cursor }
      OldCursor     := Screen.Cursor;
      Screen.Cursor := crHourglass;  try
        { copy the parameter Pascal strings to null terminated strings }
        StrPCopy(pCommandLine, CommandLine);
        StrPCopy(pInputFile, InputFile);
        StrPCopy(pOutPutFile, OutputFile);    { prepare SecAtrrs structure for the CreateFile calls.  This SecAttrs
          structure is needed in this case because we want the returned handle to
          be inherited by child process. This is true when running under WinNT.
          As for Win95, the parameter is ignored. }
        FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
        SecAtrrs.nLength              := SizeOf(SecAtrrs);
        SecAtrrs.lpSecurityDescriptor := nil;
        SecAtrrs.bInheritHandle       := TRUE;    if InputFile <> '' then
        begin
          { create the appropriate handle for the input file }
          hInputFile := CreateFile(
             pInputFile,                          { pointer to name of the file }
             GENERIC_READ or GENERIC_WRITE,       { access (read-write) mode }
             FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }
             @SecAtrrs,                           { pointer to security attributes }
             OPEN_ALWAYS,                         { how to create }
             FILE_ATTRIBUTE_NORMAL
             or FILE_FLAG_WRITE_THROUGH,          { file attributes }
             0);                                 { handle to file with attrs to copy }      { is hInputFile a valid handle? }
          if hInputFile = INVALID_HANDLE_VALUE then
            raise Exception.CreateFmt(ROUTINE_ID + #10 +  #10 +
               'WinApi function CreateFile returned an invalid handle value' + #10 +
               'for the input file * %s *' + #10 + #10 +
                ErrMsg, [InputFile]);
        end else
          { we aren't using an input file }
          hInputFile := 0;    { create the appropriate handle for the output file }
        hOutputFile := CreateFile(
           pOutPutFile,                         { pointer to name of the file }
           GENERIC_READ or GENERIC_WRITE,       { access (read-write) mode }
           FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }
           @SecAtrrs,                           { pointer to security attributes }
           CREATE_ALWAYS,                       { how to create }
           FILE_ATTRIBUTE_NORMAL
           or FILE_FLAG_WRITE_THROUGH,          { file attributes }
           0 );                                 { handle to file with attrs to copy }    { is hOutputFile a valid handle? }
        if hOutputFile = INVALID_HANDLE_VALUE then
          raise Exception.CreateFmt(ROUTINE_ID + #10 +  #10 +
             'WinApi function CreateFile returned an invalid handle value'  + #10 +
             'for the output file * %s *' + #10 + #10 +
             ErrMsg, [OutputFile]);    { prepare StartupInfo structure }
        FillChar(StartupInfo, SizeOf(StartupInfo), #0);
        StartupInfo.cb          := SizeOf(StartupInfo);
        StartupInfo.dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        StartupInfo.wShowWindow := SW_HIDE;
        StartupInfo.hStdOutput  := hOutputFile;
        StartupInfo.hStdInput   := hInputFile;    { create the app }
        Result := CreateProcess(
           NIL,                           { pointer to name of executable module }
           pCommandLine,                  { pointer to command line string }
           NIL,                           { pointer to process security attributes }
           NIL,                           { pointer to thread security attributes }
           TRUE,                          { handle inheritance flag }
           HIGH_PRIORITY_CLASS,           { creation flags }
           NIL,                           { pointer to new environment block }
           NIL,                           { pointer to current directory name }
           StartupInfo,                   { pointer to STARTUPINFO }
           ProcessInfo);                  { pointer to PROCESS_INF }    { wait for the app to finish its job and take the handles to free them later }
        if Result then
        begin
          WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
          hAppProcess  := ProcessInfo.hProcess;
          hAppThread   := ProcessInfo.hThread;
        end else
          raise Exception.Create(ROUTINE_ID + #10 +  #10 +
             'Function failure'  + #10 +  #10 + ErrMsg);  finally
        { close the handles
          Kernel objects, like the process and the files we created in this case,
          are maintained by a usage count.
          So, for cleaning up purposes we have to close the handles
          to inform the system that we don't need the objects anymore }
        if hOutputFile <> 0 then
          CloseHandle(hOutputFile);
        if hInputFile <> 0 then
          CloseHandle(hInputFile);
        if hAppThread <> 0 then
          CloseHandle(hAppThread);
        if hAppProcess <> 0 then
          CloseHandle(hAppProcess);
        { restore the old cursor }
        Screen.Cursor:= OldCursor;
      end;
    end;    { CreateDOSProcessRedirected } 
      

  8.   

    Redirecting DOS App Output  D2 D3 D4  
    A function to execute a DOS or Win32 consoloe mode application and wait for it to close before continuing. Input for the app can be directed from a file, and the output will be redirected to a file.  
    uses
      Controls, Windows, SysUtils, Forms;{---------------------------CreateDOSProcessRedirected--------------------------
     Description    : executes a (DOS!) app defined in the CommandLine parameter
                      redirected to take input from InputFile (optional) and give
                      output to OutputFile
     Result         : True on success
     Parameters     : CommandLine : the command line for app, including full path
                      InputFile   : the ascii file where from the app takes input,
                                     empty if no input needed/required.
                      OutputFile  : the ascii file to which the output is redirected
                      ErrMsg      : additional error message string. Can be empty
     Error checking : YES
     Target         : Delphi 2, 3, 4
     Author         : Theodoros Bebekis, email [email protected]
     Notes          :
     Example call   : CreateDOSProcessRedirected('C:\MyDOSApp.exe',
                                                 'C:\InputPut.txt',
                                                 'C:\OutPut.txt',
                                                 'Please, record this message')
    -------------------------------------------------------------------------------}
    function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile,
       ErrMsg :string): boolean;
    const
      ROUTINE_ID = '[function: CreateDOSProcessRedirected]';
    var
      OldCursor     : TCursor;
      pCommandLine  : array[0..MAX_PATH] of char;
      pInputFile,
      pOutPutFile   : array[0..MAX_PATH] of char;
      StartupInfo   : TStartupInfo;
      ProcessInfo   : TProcessInformation;
      SecAtrrs      : TSecurityAttributes;
      hAppProcess,
      hAppThread,
      hInputFile,
      hOutputFile   : THandle;
    begin
      Result := FALSE;  { check for InputFile existence }
      if (InputFile <> '') and (not FileExists(InputFile)) then
        raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
           'Input file * %s *' + #10 +
           'does not exist' + #10 + #10 +
           ErrMsg, [InputFile]);  hAppProcess := 0;
      hAppThread := 0;
      hInputFile := 0;
      hOutputFile := 0;  { save the cursor }
      OldCursor     := Screen.Cursor;
      Screen.Cursor := crHourglass;  try
        { copy the parameter Pascal strings to null terminated strings }
        StrPCopy(pCommandLine, CommandLine);
        StrPCopy(pInputFile, InputFile);
        StrPCopy(pOutPutFile, OutputFile);    { prepare SecAtrrs structure for the CreateFile calls.  This SecAttrs
          structure is needed in this case because we want the returned handle to
          be inherited by child process. This is true when running under WinNT.
          As for Win95, the parameter is ignored. }
        FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
        SecAtrrs.nLength              := SizeOf(SecAtrrs);
        SecAtrrs.lpSecurityDescriptor := nil;
        SecAtrrs.bInheritHandle       := TRUE;    if InputFile <> '' then
        begin
          { create the appropriate handle for the input file }
          hInputFile := CreateFile(
             pInputFile,                          { pointer to name of the file }
             GENERIC_READ or GENERIC_WRITE,       { access (read-write) mode }
             FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }
             @SecAtrrs,                           { pointer to security attributes }
             OPEN_ALWAYS,                         { how to create }
             FILE_ATTRIBUTE_NORMAL
             or FILE_FLAG_WRITE_THROUGH,          { file attributes }
             0);                                 { handle to file with attrs to copy }      { is hInputFile a valid handle? }
          if hInputFile = INVALID_HANDLE_VALUE then
            raise Exception.CreateFmt(ROUTINE_ID + #10 +  #10 +
               'WinApi function CreateFile returned an invalid handle value' + #10 +
               'for the input file * %s *' + #10 + #10 +
                ErrMsg, [InputFile]);
        end else
          { we aren't using an input file }
          hInputFile := 0;    { create the appropriate handle for the output file }
        hOutputFile := CreateFile(
           pOutPutFile,                         { pointer to name of the file }
           GENERIC_READ or GENERIC_WRITE,       { access (read-write) mode }
           FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }
           @SecAtrrs,                           { pointer to security attributes }
           CREATE_ALWAYS,                       { how to create }
           FILE_ATTRIBUTE_NORMAL
           or FILE_FLAG_WRITE_THROUGH,          { file attributes }
           0 );                                 { handle to file with attrs to copy }    { is hOutputFile a valid handle? }
        if hOutputFile = INVALID_HANDLE_VALUE then
          raise Exception.CreateFmt(ROUTINE_ID + #10 +  #10 +
             'WinApi function CreateFile returned an invalid handle value'  + #10 +
             'for the output file * %s *' + #10 + #10 +
             ErrMsg, [OutputFile]);    { prepare StartupInfo structure }
        FillChar(StartupInfo, SizeOf(StartupInfo), #0);
        StartupInfo.cb          := SizeOf(StartupInfo);
        StartupInfo.dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        StartupInfo.wShowWindow := SW_HIDE;
        StartupInfo.hStdOutput  := hOutputFile;
        StartupInfo.hStdInput   := hInputFile;    { create the app }
        Result := CreateProcess(
           NIL,                           { pointer to name of executable module }
           pCommandLine,                  { pointer to command line string }
           NIL,                           { pointer to process security attributes }
           NIL,                           { pointer to thread security attributes }
           TRUE,                          { handle inheritance flag }
           HIGH_PRIORITY_CLASS,           { creation flags }
           NIL,                           { pointer to new environment block }
           NIL,                           { pointer to current directory name }
           StartupInfo,                   { pointer to STARTUPINFO }
           ProcessInfo);                  { pointer to PROCESS_INF }    { wait for the app to finish its job and take the handles to free them later }
        if Result then
        begin
          WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
          hAppProcess  := ProcessInfo.hProcess;
          hAppThread   := ProcessInfo.hThread;
        end else
          raise Exception.Create(ROUTINE_ID + #10 +  #10 +
             'Function failure'  + #10 +  #10 + ErrMsg);  finally
        { close the handles
          Kernel objects, like the process and the files we created in this case,
          are maintained by a usage count.
          So, for cleaning up purposes we have to close the handles
          to inform the system that we don't need the objects anymore }
        if hOutputFile <> 0 then
          CloseHandle(hOutputFile);
        if hInputFile <> 0 then
          CloseHandle(hInputFile);
        if hAppThread <> 0 then
          CloseHandle(hAppThread);
        if hAppProcess <> 0 then
          CloseHandle(hAppProcess);
        { restore the old cursor }
        Screen.Cursor:= OldCursor;
      end;
    end;    { CreateDOSProcessRedirected } 
      

  9.   

    Open Windows Default About Box
    The ShellApi unit contains some good functions. Among the more exotic are this one, ShellAbout. With this function you can open the default Windows About Box with your own text: 
    uses
      ShellApi;begin
      ShellAbout( self.Handle, pChar('This is my header'), 
                  pChar('This is my copyright'), HICON(nil) 
                );
    end;If you wish to include your own icon, let HICON(nil) point to a icon handle. When pointing to nil the default Windows ison is used.
      

  10.   

    Open Windows Default About Box
    The ShellApi unit contains some good functions. Among the more exotic are this one, ShellAbout. With this function you can open the default Windows About Box with your own text: 
    uses
      ShellApi;begin
      ShellAbout( self.Handle, pChar('This is my header'), 
                  pChar('This is my copyright'), HICON(nil) 
                );
    end;If you wish to include your own icon, let HICON(nil) point to a icon handle. When pointing to nil the default Windows ison is used.
      

  11.   

    i am in complete accord with the idea