c# - FileStream.close() does not free file for other processes -
i have next code in page_load called function. when page loaded first time after starting visual studio, works out fine. other opening phone call file after returns ioexception: "file in utilize process"
, when straight opening file in visualstudio solution error returned(of course of study not exception)
filestream mailinglist_filestream = new filestream(@"\foobarfile.txt", filemode.open); peekingstreamreader mailinglist_reader = new peekingstreamreader(mailinglist_filestream); //do stuff file mailinglist_filestream.close(); mailinglist_reader.close(); mailinglist_reader.dispose(); mailinglist_filestream.dispose();
why file still locked? , why restarting visual studio reset file? when checking file-properties says:
build action: content re-create output directory: not copy
i reading file. can similiar adlockoptimistic
, multiple processes can access file?
why file still locked? , why restarting visual studio reset file? when checking file-properties says [...] don't know why file still locked: because code fails before stream closed/disposed.
about "why restarting visual studio [...]": because may using iis express or asp.net dev server closed when close ide, locks on files released since process holding locks no longer running.
and "why file still locked?[...]" because file stream isn't closed because thread may not end , locks aren't released.
as other reply said, check how using
block may avoid idisposable
objects wouldn't disposed:
// fileshare.readwrite allow other processes // read , write target file if other processes // working same file using (filestream mailinglist_filestream = new filestream(@"\foobarfile.txt", filemode.open, fileshare.readwrite)) using (peekingstreamreader mailinglist_reader = new peekingstreamreader(mailinglist_filestream)) { // stuff. using blocks phone call dispose() // if goes wrong, it's equal try/finally! // check how using statements can chained without { } }
i reading file. can similiar adlockoptimistic, multiple processes can access file?
yes, take @ file.open
method , fileshare
enumeration:
file.open
: http://msdn.microsoft.com/en-us/library/y973b725.aspx fileshare
enum: http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx c# asp.net filestream file-access
No comments:
Post a Comment