Stephen Posey wrote: > Rich Cooper wrote: >> Thanks Stephen, >> >> I found a web page with a similar snippet, and so I was able to >> get the following to work in my OnFormCreate handler: >> >> >> D := GetStartDir; >> ShellExecute( 0, 'open', PChar('command.com'), >> PChar('/c '+'ipconfig/all > harbor.txt'), >> nil, SW_HIDE ); >> Waiting := 100; >> while ( (Waiting>0) and (not FileExists(D+'HARBOR.TXT'))) >> do begin >> Sleep(250); >> Waiting := Waiting-1; >> end; >> >> if (not FileExists(D+'HARBOR.TXT')) >> then raise Exception.Create('Login Error 1.'); >> >> But there is still a problem. The 'Waiting' loop discovers that the >> file exists, but not whether it has been completely written and let >> loose to be read. In my OnFormActivate handler, I use: >> >> if FirstActivation >> then begin >> D := GetStartDir; >> Application.ProcessMessages; >> Sleep(500); >> >> if FileExists(D+'HARBOR.TXT') >> then begin >> meHarbor.Lines.LoadFromFile(D+'HARBOR.TXT'); >> >> >> which works 95% of the time. But sometimes it causes the LoadFromFile >> procedure to throw an exception related to trying to read a file that is >> not yet ready to be read - still locked by the NT file system in XP, and >> not >> yet completely written to the HARBOR.TXT file and then released to >> the rest of the computer. >> >> Does anyone have a way to determine whether the file HARBOR.TXT >> has been released for reading after being completely written? I would >> be able to fix this problem by replacing the Sleep(500) with a loop >> that tests till the HARBOR.TXT file is ready for reading, having been >> fully released. > > I don't know precisely how cmd.exe creates and opens a redirected file > like that, but your attempting to open it in "exclusive share" mode > ought to fail regardless if the file is still being written. > > You can use a TFileStream instance, like so: > > FS := TFileStream.Create(D + 'HARBOR.TXT', > fmOpenRead or fmShareExclusive); > > Attempt that and catch any exception raised due to the sharing conflict > (should be some form of EInOutError), if the file exists and no > exception is raised on attempting to open it, then it should be finished. > > HTH > > Stephen Posey > [EMAIL PROTECTED]
I tried "LoadFromFile()" with try ... except end around it and kept looping till the exceptions went away - that worked just fine! Thanks Stephen and Simon, Rich _______________________________________________ Delphi mailing list -> Delphi@elists.org http://www.elists.org/mailman/listinfo/delphi