Minimize the LOCK time of the file
I am working on a applicat开发者_StackOverflowion which reads a file and after processing saves it back. I have to keep file in a stream while the file is in the processing. Now the issue is that I have to minimize the LOCK time of the file.
What is the solution?I would recommend you to keep your file in MemoryStream
. When you write to a MemoryStream
, the data is stored in memory instead of being stored to the file system. After the processing you can then call MemoryStream.WriteTo
to store the data on the file system permanently. This will minimize the lock time.
You can open the file handle with a ShareMode of which allows other processes to access the file. In other words, you can read the file without creating an exclusive lock:
FileStream stream = File.Open("path", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
精彩评论