Board index » delphi » File Access Denied after copying file

File Access Denied after copying file

Hi,

I have a rather strange problem, for which I have tried many different work
arounds, but to no avail. The problem is this - I am copying a text file,
using TFileStreams, and then I am trying to alter the copied version of the
file as a System.Text file. When I try a call to Reset on the file, I get a
File Access Denied error. The strange thing is, that if the file already
existed in the destination directory before copying the file, then it
happily allows me access to it.

I am copying the file using:

procedure FileCopy(Const SourceFile, DestFile:String);
var S,T :TFileStream;
begin
 S:=TFileStream.Create(SourceFile, fmOpenRead);
 try
  T:=TFileStream.Create(DestFile, fmOpenWrite or fmCreate);

  try
   T.CopyFrom(S, S.Size);
  finally
   T.Free;
  end;
 finally
  S.Free;
 end;
end;

.... and then I am attempting to change the file in the following way:

function TAppLockFile.GetSerialNumber:String;
var UserFile:System.Text;
    S:String;
    I,J:Integer;
begin
 If FFileName='' then
  raise Exception.Create('File Name Not Specified');

 Result:='';
 try
  System.Assign(UserFile,FFileName);

  Reset(UserFile);

.... but the call to Reset causes a File Access Denied error.

Any ideas?

TIA

Ben Phillips
BPhilll...@EventManagementSystems.co.uk

 

Re:File Access Denied after copying file


On Tue, 3 Nov 1998 18:23:18 -0000, "Ben Phillips"

Quote
<bphill...@eventmanagementsystems.co.uk> wrote:

[snip]
Quote
>The problem is this - I am copying a text file,
>using TFileStreams, and then I am trying to alter the copied version of the
>file as a System.Text file. When I try a call to Reset on the file, I get a
>File Access Denied error.

[snip]

Quote
>I am copying the file using:

>procedure FileCopy(Const SourceFile, DestFile:String);
>var S,T :TFileStream;
>begin
> S:=TFileStream.Create(SourceFile, fmOpenRead);
> try
>  T:=TFileStream.Create(DestFile, fmOpenWrite or fmCreate);

>  try
>   T.CopyFrom(S, S.Size);
>  finally
>   T.Free;
>  end;
> finally
>  S.Free;
> end;
>end;

>.... and then I am attempting to change the file in the following way:

>function TAppLockFile.GetSerialNumber:String;
>var UserFile:System.Text;
>    S:String;
>    I,J:Integer;
>begin
> If FFileName='' then
>  raise Exception.Create('File Name Not Specified');

> Result:='';
> try
>  System.Assign(UserFile,FFileName);

>  Reset(UserFile);

>.... but the call to Reset causes a File Access Denied error.

>Any ideas?

Are you calling FileOpen and FileClose on the file that you are
passing to the TFileStream class instance?  Be aware that if FileClose
is not called, the WinAPI will have created a filehandle which has set
the filemode to what you specified (e.g. share deny) which remains
active until it is released via FileClose.

Alternatively make sure the FileMode variable is set to 2 = Read/Write
before accessing typed and untyped files opened by the Reset class.

Valid FileMode values are:

0       Read only
1       Write only
2       Read/Write

Other Threads