Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

How to make a copy of file thath are in use by the system

Hello  Experts,

I need know if exists any way to make a copy  of file thath are in use by windows (with delphi app) , like the files in <windowsdir>/system32/config and others.

Thanks in advance.
0
MauricioGaviria
Asked:
MauricioGaviria
4 Solutions
 
gmayoCommented:
Depends whether the file is open with denied access, in which case you can't. If it is shared access then you should be able to open it in read-only mode. If you are trying to use AssignFile, Reset etc then you need to set the FileMode variable to 0 (read-only). By default it is 2 (read-write) which fails when trying to open a file that is in use.

Does CopyFile not work?

Geoff M.
0
 
nestoruaCommented:
HI,
I guess you simply want to copy password/security files like sam, security, system, default ets.? Am I right?
Sincerely,
Nestorua.
0
 
DeerBearCommented:
Hi,

Try CopyFileEx.

HTH,

Andrew
0
 
fibdevCommented:
this may or may not work, but you can try...

procedure TForm1.DoCopy(Source, Dest: string);
var
  SrcFile : Integer;
  DestFile : Integer;
  S : string;
  RetCode : Longint;
  OpenFileBuf   : TOFStruct;
  FName : array[ 0..255 ] of Char;
begin
  StrPCopy( FName, Source );
  SrcFile := LZOpenFile( FName, OpenFileBuf, of_Read );
  StrPCopy( FName, Dest );
  DestFile := LZOpenFile( FName, OpenFileBuf, of_Create );

  RetCode := LZCopy( SrcFile, DestFile );
  if RetCode >= 0 then
  begin
    LZClose( SrcFile );
    LZClose( DestFile );
  end
  else
  begin
    Str( RetCode, S );
    MessageDlg( 'Could not copy ' + Source + ' to ' +
                Dest + #13 + 'Error Code = ' + S, mtError, [mbOk], 0 );
  end;
end;
0
 
unfunfCommented:
CopyFile('TargetFile', 'NewFileName', False);  // The false is for 'Fail if exists'

If I am not mistaken, CopyFile works perfectly fine on files that are in use.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now