मैं TFilestream का उपयोग कर नेटवर्क शेयर (स्थानीय) पर लिखने का प्रयास करता हूं। नेटवर्क कनेक्शन को हस्तक्षेप नहीं किया जाना चाहिए यह सब ठीक काम करता है।TFilestream का उपयोग कर नेटवर्क साझा करने के लिए डेल्फी लेखन फ़ाइल खो जाने पर फ़ाइल को ताला लगा देता है
हालांकि, अगर मैं नेटवर्क केबल खींचता हूं और फिर इसे फिर से कनेक्ट करता हूं, तो प्रवेश प्रतिबंधों के कारण फ़ाइलस्ट्रीम खोलने के बाद के प्रयास विफल हो जाते हैं। मैं एक्सप्लोरर में फ़ाइल को भी हटा नहीं सकता! ऐसा लगता है कि TFilestream फ़ाइल को लॉक करता है और इसे पाने के लिए एकमात्र तरीका रीबूट करना है।
मेरे आवेदन में, मैं फ़ाइल को पूरे समय खोलता हूं जब मैं इसे लिख रहा हूं (यह हर बार एक बार एक लॉग फ़ाइल लिखा जाता है)।
मेरे कोड है जो विफल रहता है नीचे है: यह सराहना की जाएगी
procedure TFileLogger.SetLogFilename(const Value: String);
var line : String;
Created : Boolean;
begin
if not DirectoryExists(ExtractFilePath(Value)) then //create the dir if it doesnt exist
begin
try
ForceDirectories(ExtractFilePath(Value));
except
ErrorMessage(Value); //dont have access to the dir so flag an error
Exit;
end;
end;
if Value <> FLogFilename then //Either create or open existing
begin
Created := False;
if Assigned(FStream) then
FreeandNil(FStream);
if not FileExists(Value) then //create the file and write header
begin
//now create a new file
try
FStream := TFileStream.Create(Value,fmCreate);
Created := True;
finally
FreeAndNil(FStream);
end;
if not Created then //an issue with creating the file
begin
ErrorMessage(Value);
Exit;
end;
FLogFilename := Value;
//now open file for writing
FStream := TFileStream.Create(FLogFilename,fmOpenWrite or fmShareDenyWrite);
try
line := FHeader + #13#10;
FStream.Seek(0,soFromEnd);
FStream.Write(Line[1], length(Line));
FSuppress := False;
except
ErrorMessage(Value);
end;
end else begin //just open it
FLogFilename := Value;
//now open file for writing
FStream := TFileStream.Create(FLogFilename,fmOpenWrite or fmShareDenyWrite); //This line fails if the network is lost and then reconnected
end;
end;
end;
अगर कोई किसी भी सलाह है।
क्या यह वास्तव में TFileStream के साथ एक समस्या है? यदि ऐसा है, तो बस CreateFile की तरह कुछ और उपयोग करें। –