[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Problems copy file from CD using Block Read - Access Denied

I have written an install program, which gets a destination path from a user and then copies a database (mdb) file from a CD to the destination.

The install program works when I test it from the hard disk, but when I burnt it to a CD and went to test it, the database file was not copied.

I put some dialogs to show me what was happening, and I get an error msg re: Access Denied

Code below

=====================
var
  FromF, ToF : file of byte;
  Buffer : array[ 0..4096 ] of char;
  NumRead : integer;
  FileLength : longint;

begin
  try
   AssignFile( FromF, Source );
   filemode := fmOpenReadWrite;
   Reset( FromF );
   AssignFile( ToF, Destination);
   Rewrite( ToF );
   FileLength := FileSize( FromF );

      while FileLength > 0 do begin
        BlockRead( FromF, Buffer[ 0 ], SizeOf( Buffer ), NumRead );
        FileLength := FileLength - NumRead;
        BlockWrite( ToF, Buffer[ 0 ], NumRead );
        Position := Position + NumRead;
      end;
 
   CloseFile( FromF );
   CloseFile( ToF );
  except
        on e: Exception do
              MessageDlg('Error :'+e.Message, mtError, [mbOK], 0);
  end;  // try/except
end;
0
pjelias
Asked:
pjelias
  • 2
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
Your code:

   filemode := fmOpenReadWrite;
   Reset( FromF );


opens the source file in read / write mode, which is not possible on a RO (readonly) media. It really should be:

   filemode := fmOpenRead;
   Reset( FromF );


---------

Russell
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Btw, you could also just use the CopyFile routine in windows:

 if not(CopyFile(PChar(Source), PChar(Destination), False)) then
 begin
    // GetLastError will contain the error code
    ShowMessage(SysErrorMessage(GetLastError));
 end;

---

Russell
0
 
pjeliasAuthor Commented:
Of course, hard to see the simple things at 2:30am.

Also need to set the File Attributes so that file is not read only.

Thanks for your help

Regards
pje
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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