In WinApi bestaat FileExists niet Hoe kan ik snel controleren of een bestand bestaat?
De VCL doet het heel erg omslachtig.
In WinApi bestaat FileExists niet Hoe kan ik snel controleren of een bestand bestaat?
De VCL doet het heel erg omslachtig.
"Some are wise, some are otherwise."
FindFirstFile gebruiken; als je 0 terugkrijgt bestaat het bestand... (en vergeet FindClose niet)
Edit: dit was dus niet correct, sorry
Last edited by PsychoMark; 26-Jan-05 at 15:30.
Qui custodiet ipsos custodes
En als ik het goed heb heb je vanaf Win2K een probleem als je FindClose aanroept terwijl je FindFirstFile iets anders dan 0 terugkrijgt
!
Hoe moet ik dat dan oplossen SvG?
"Some are wise, some are otherwise."
Code:function FileExists(const FileName: string): Boolean; var Handle: THandle; FindData: TWin32FindData; begin Handle := Windows.FindFirstFile(PChar(FileName), FindData); Result := (Handle <> INVALID_HANDLE_VALUE); if Result then Windows.FindClose(Handle); end;
Qui custodiet ipsos custodes
Ik wilde net posten dat ik het gevonden had
"Some are wise, some are otherwise."
...de code van FileAge overnemen en inkorten, inderdaad...
Always code as if the person who ends up maintaining your code
will be a violent psychopath who knows where you live.
John F. Woods (quoted on Usenet) 1997/08/20
Als het klopt wat SvG zegt, is je code verkeerd Psycho.
Jouw code:Resulteert in True als Handle bestaat. DusCode:Result := (Handle <> INVALID_HANDLE_VALUE);laat de app crashen (dat gebeurde bij mij).Code:if Result then Windows.FindClose(Handle);
Dus dit zou het dan moeten zijn:
Code:Result := (Handle = INVALID_HANDLE_VALUE);
"Some are wise, some are otherwise."
Als Handle INVALID_HANDLE_VALUE is dan bestaat het bestand niet, dus moet Result False worden en er geen FindClose aangeroepen worden... met jouw laatste code zal FileExists juist True teruggeven als het bestand niet bestaat...
Crasht je applicatie echt op FindClose dan?
Qui custodiet ipsos custodes
Dit is jouw code. Result wordt True als Handle is niet INVALID_HANDLE_VALUE, dus hij wordt true als het bestand bestaat. SvG zegt dat het programma crasht als er FindClose wordt aangeroepen terwijl hij het bestand WEL gevonden heeft.Code:Result := (Handle <> INVALID_HANDLE_VALUE);
Een van ons is dus in de warEn als ik het goed heb heb je vanaf Win2K een probleem als je FindClose aanroept terwijl je FindFirstFile iets anders dan 0 terugkrijgt
Ik neem aan dat FindFirstFile 0 is als er iets fout gaat.
"Some are wise, some are otherwise."
SysUtils.FindFirst: als er 0 terug gegeven wordt bestaat het bestand wel
Windows.FindFirstFile: als er INVALID_HANDLE_VALUE terug gegeven wordt bestaat het bestand niet
...SVG had het waarschijnlijk over de SysUtils variant. Het verschil is dat de API een Handle teruggeeft, en geen status (de SysUtils variant geeft bij een fout GetLastError terug, dit kan omdat de Handle opgeslagen wordt in TSearchRec). Mijn code blijft voorlopig dus correct; indien de Handle niet INVALID_HANDLE_VALUE is bestaat het bestand wel en moet dus ook FindClose aangeroepen worden...
Qui custodiet ipsos custodes
Ok, dat verklaart
Ik vond het al raar.
"Some are wise, some are otherwise."
F.Y.I:
Uit de win32 help:
En uit Windows.pasIf the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
Code:const INVALID_HANDLE_VALUE = DWORD(-1);
Always code as if the person who ends up maintaining your code
will be a violent psychopath who knows where you live.
John F. Woods (quoted on Usenet) 1997/08/20
Ik ga met mark mee
Ik raak in de war omdat er eerst werd gezegt dat FindFirstFile 0 teruggeeft als het bestand bestaat.. Daarop zeg ik dat je findClose niet moet aanroepen als je iets anders dan 0 hebt gekregen
!
Maar dan nog. Het kan toch geen kwaad om FindClose aan te roepen? Ik kan me niet voorstellen dat dat problemen oplevert.
1+1=b
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks