Sometimes it might get handy to read some file that may remain in use for very long time by other processes, one possible example of such process involves log files they are mostly locked by their parent processes and sometimes we need to read the content of file without stopping the main process. There could be two possible ways to read an locked file(locked means some other process have exclusive access to that file)
1. Create a Copy of that file using Snapshot copy of volume of course this option is not the optimal solution in case of large files and even snapshot copy is an easy task that can be managed through .net code easily.
2. Other option that i came across and works for me is by opening file in read/shared mode. You can use this method specially with log4net which don't acquire lock on file for a very long time.
1. Create a Copy of that file using Snapshot copy of volume of course this option is not the optimal solution in case of large files and even snapshot copy is an easy task that can be managed through .net code easily.
2. Other option that i came across and works for me is by opening file in read/shared mode. You can use this method specially with log4net which don't acquire lock on file for a very long time.
FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader logFileReader = new StreamReader(logFileStream); while (!logFileReader.EndOfStream) { string line = logFileReader.ReadLine(); Console.WriteLine(line); } // Clean up logFileReader.Close(); logFileStream.Close();
No comments:
Post a Comment