Delphi how to check if you have read permission on a directory

It seems to be difficult to check the read permission on a directory in Delphi 2009. Here we will see how to find the read permission in an alternate way. In later version of Delphi we have more straight way of achieving this.

Here we will try to list the files in the directory and will check the status of this to decide whether the user has read permission to this drive. We must also consider whether the directory exists as well since we are doing this in an alternate manner. Below is the code which will help us decide on whether the user has permission on the given folder.

How does it work:

We will try to list the files and folders in the directory. If the folder exists, then the FindFirst operation will return 0. If the folder does not exist, then it will return a different result. Once it returns 0, we will check the first items Attr property, this will tell us whether it is a folder or a file or something else. The first item in the result will always be the current folder. If we check the Attr then it should return a status faDirectory which has a value 16 which says it is a directory. now lets see how to check whether the folder has read permission. To do this we have to check if the Attr property is returning code 1040 or not. If the first item Attr says it is 1040 then that means we don’t have read permission on that drive.

function HavePermissionToDrive(directory : string) : Boolean;
var
SR: TSearchRec;
faNotAllowed : Integer;
begin
faNotAllowed := 1040; //custom code for none.
Result := true;

if FindFirst(directory + '\*.*', faAnyFile, SR) = 0 then
begin
  if(SR.Attr = faNotAllowed) then
  begin
   Result := false;
  end;
  end
else
 Result := false;
end;

Setting Up Kafka

Apache Kafka is a high-throughput, low-latency event processing system designed to handle millions of data feeds per second. It has the c...… Continue reading

Libish Varghese Jacob

Libish Varghese JacobI am a lead engineer at a prominent wind turbine manufacturing firm. My interests span a diverse range, and immersing myself in technology is one of them. This platform serves as my primary knowledge base, where I seek information and insights. Moreover, I utilize this platform to share my experiences and experiments, hoping they may benefit those following a similar path. It's important to note that the suggestions I express here are based on my best knowledge at the time of writing and may not necessarily represent the optimal solutions. I wholeheartedly welcome any comments you may have to improve or refine my ideas. Your feedback is greatly appreciated and valued.