filesystems - How to find out if a directory exists on Delphi XE2 *correctly*? -
i need check if directory exists! if directory "e:\test" e: cd/dvd drive , there no disk inserted on it, see following delphi , windows issues.
the first method:
function direxists(name: string): boolean; var code: integer; begin code := getfileattributesw(pchar(name)); result := (code <> -1) , (file_attribute_directory , code <> 0); end;
it gives range check error
. can't use {$rangechecks off}
, {$rangechecks on}
blocks because:
- it breaks current state of
$rangechecks
option. - we see system error
drive not ready
insteadrange check error
. need check if directory exists without error dialogs user.
the second method:
if directoryexists(name, true) ...
this function returns true
non-existing e:\test
directory on empty cd/dvd drive. can't use because works incorrectly.
but then, how find out if directory exists?
p.s. think error exists cd/dvd drive. using windows 7 x64 on vmware fusion 5 under mac os x 10.8.4 external cd/dvd drive.
you can fix function doesn't cause range check errors:
function direxists(name: string): boolean; var code: dword; begin code := getfileattributes(pchar(name)); result := (code <> invalid_file_attributes) , (file_attribute_directory , code <> 0); end;
the range check errors due mixing signed , unsigned types. remy points out useful trick set compiler options , restore prevailing state. that's trick learn, don't need here.
the xe3 implementation of directoryexists modified address problem have encountered. so, if using xe3+ option, should take it.
to suppress system error dialogs call @ process startup:
procedure setprocesserrormode; var mode: dword; begin mode := seterrormode(sem_failcriticalerrors); seterrormode(mode or sem_failcriticalerrors); end;
doing best practise described on msdn:
best practice applications call process- wide seterrormode function parameter of sem_ failcriticalerrors @ startup. prevent error mode dialogs hanging application.
Comments
Post a Comment