开发者

Check if a directory is readable

How we can check if a directory is readOnl开发者_如何学Cy or Not?


you can use the FileGetAttr function and check if the faReadOnly flag is set.

try this code

function DirIsReadOnly(Path:string):Boolean;
var
 attrs    : Integer;
begin
 attrs  := FileGetAttr(Path);
 Result := (attrs  and faReadOnly) > 0;
end;


Testing if the directory's attribute is R/O is only part of the answer. You can easily have a R/W directory that you still can't write to - because of Access Rights.

The best way to check if you can write to a directory or not is - to try it:

FUNCTION WritableDir(CONST Dir : STRING) : BOOLEAN;
  VAR
    FIL : FILE;
    N   : STRING;
    I   : Cardinal;

  BEGIN
    REPEAT
      N:=IncludeTrailingPathDelimiter(Dir);
      FOR I:=1 TO 250-LENGTH(N) DO N:=N+CHAR(RANDOM(26)+65)
    UNTIL NOT FileExists(N);
    Result:=TRUE;
    TRY
      AssignFile(FIL,N);
      REWRITE(FIL,1);
      Result:=FileExists(N); // Not sure if this is needed, but AlainD says so :-)
    EXCEPT
      Result:=FALSE
    END;
    IF Result THEN BEGIN
      CloseFile(FIL); 
      ERASE(FIL)
    END
  END;


The version HeartWare has given is nice but contains two bugs. This modified versions works more reliably and has comments to explain what is going on:

function IsPathWriteable(const cszPath: String) : Boolean;
var
    fileTest: file;
    szFile: String;
    nChar: Cardinal;
begin
    // Generate a random filename that does NOT exist in the directory
    Result := True;
    repeat
        szFile := IncludeTrailingPathDelimiter(cszPath);
        for nChar:=1 to (250 - Length(szFile)) do
            szFile := (szFile + char(Random(26) + 65));
    until (not FileExists(szFile));

    // Attempt to write the file to the directory. This will fail on something like a CD drive or
    // if the user does not have permission, but otherwise should work.
    try
        AssignFile(fileTest, szFile);
        Rewrite(fileTest, 1);

        // Note: Actually check for the existence of the file. Windows may appear to have created
        // the file, but this fails (without an exception) if advanced security attibutes for the
        // folder have denied "Create Files / Write Data" access to the logged in user.
        if (not FileExists(szFile)) then
            Result := False;
    except
        Result := False;
    end;

    // If the file was written to the path, delete it
    if (Result) then
        begin
        CloseFile(fileTest);
        Erase(fileTest);
        end;
end;


In Windows API way, it is:

fa := GetFileAttributes(PChar(FileName))
if (fa and FILE_ATTRIBUTE_DIRECTORY <> 0) and (fa and FILE_ATTRIBUTE_READONLY <> 0) then
  ShowMessage('Directory is read-only');


One possible way is to try list the files in that directory and check for the status. This way we can check whether it is readable. this answer is applicable to 2009 or lower. Remember we have to check whether the folder exists and then whether the folder is readable. you can find the implementation here http://simplebasics.net/delphi-how-to-check-if-you-have-read-permission-on-a-directory/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜