How to read files that are exclusively locked by other applications?<p align=left><font face=Arial size=2>Hi,</font></p> <p align=left>I am trying to read log files that are updated by a daemon process on the server. The daemon process is an COM application writing to the files exclusively. How can I read the files? To simulate the workflow, I have created the following snippet, which reproduces the problem I am facing.</p> <p align=left>I get the exception : <em>&quot;The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process.&quot;</em></p> <p align=left> </p><font size=2> <p></font><font size=2> </p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">static void Main(string[] args)  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>FileStream </font><font style="color:red">exclusiveWriter</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> FileStream(@&quot;C:\Temp\FileLockTest1.txt&quot;, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>for (byte </font><font style="color:red">counter</font><font style="font-size:11px"> = </font><font style="color:blue">0</font><font style="font-size:11px">; counter </font><font style="color:blue">&lt;</font><font style="font-size:11px"> </font><font style="font-weight:bold;color:black">100</font><font style="font-size:11px">; counter++)  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>exclusiveWriter.WriteByte(counter);  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>FileStream </font><font style="color:red">sharedReader</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> FileStream(@&quot;C:\Temp\FileLockTest1.txt&quot;, FileMode.Open, FileAccess.Read, FileShare.Read);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>sharedReader.Seek(0, SeekOrigin.Begin);  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>for (byte </font><font style="color:red">counter</font><font style="font-size:11px"> = </font><font style="color:blue">0</font><font style="font-size:11px">; counter </font><font style="color:blue">&lt;</font><font style="font-size:11px"> </font><font style="font-weight:bold;color:black">100</font><font style="font-size:11px">; counter++)  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>Console.WriteLine(sharedReader.ReadByte());  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>exclusiveWriter.Close();  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>sharedReader.Close();  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>Console.ReadLine();  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div> <p> </p> <p align=left>Thanks in advance,</p> <p align=left>- Johnson</font></p>© 2009 Microsoft Corporation. All rights reserved.Thu, 19 Jun 2008 00:15:52 Z26f31d41-c1ad-4fa0-b00e-e891a7ab4739http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#26f31d41-c1ad-4fa0-b00e-e891a7ab4739http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#26f31d41-c1ad-4fa0-b00e-e891a7ab4739John DeSouzahttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20DeSouzaHow to read files that are exclusively locked by other applications?<p align=left><font face=Arial size=2>Hi,</font></p> <p align=left>I am trying to read log files that are updated by a daemon process on the server. The daemon process is an COM application writing to the files exclusively. How can I read the files? To simulate the workflow, I have created the following snippet, which reproduces the problem I am facing.</p> <p align=left>I get the exception : <em>&quot;The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process.&quot;</em></p> <p align=left> </p><font size=2> <p></font><font size=2> </p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">static void Main(string[] args)  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>FileStream </font><font style="color:red">exclusiveWriter</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> FileStream(@&quot;C:\Temp\FileLockTest1.txt&quot;, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>for (byte </font><font style="color:red">counter</font><font style="font-size:11px"> = </font><font style="color:blue">0</font><font style="font-size:11px">; counter </font><font style="color:blue">&lt;</font><font style="font-size:11px"> </font><font style="font-weight:bold;color:black">100</font><font style="font-size:11px">; counter++)  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>exclusiveWriter.WriteByte(counter);  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>FileStream </font><font style="color:red">sharedReader</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> FileStream(@&quot;C:\Temp\FileLockTest1.txt&quot;, FileMode.Open, FileAccess.Read, FileShare.Read);  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>sharedReader.Seek(0, SeekOrigin.Begin);  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>for (byte </font><font style="color:red">counter</font><font style="font-size:11px"> = </font><font style="color:blue">0</font><font style="font-size:11px">; counter </font><font style="color:blue">&lt;</font><font style="font-size:11px"> </font><font style="font-weight:bold;color:black">100</font><font style="font-size:11px">; counter++)  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>Console.WriteLine(sharedReader.ReadByte());  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>exclusiveWriter.Close();  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>sharedReader.Close();  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>Console.ReadLine();  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div> <p> </p> <p align=left>Thanks in advance,</p> <p align=left>- Johnson</font></p>Fri, 02 May 2008 06:11:10 Z2008-06-03T09:48:54Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#2548602b-c07f-46b6-b373-15afd4800eadhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#2548602b-c07f-46b6-b373-15afd4800eadEyalShilonyhttp://social.msdn.microsoft.com/Profile/en-US/?user=EyalShilonyHow to read files that are exclusively locked by other applications?<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>John DeSouza wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p align=left><font face=Arial size=2>Hi,</font></p> <p align=left>I am trying to read log files that are updated by a daemon process on the server. The daemon process is an COM application writing to the files exclusively. How can I read the files? To simulate the workflow, I have created the following snippet, which reproduces the problem I am facing.</p> <p align=left>I get the exception : <em>&quot;The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process.&quot;</em></p> <p align=left> </p><font size=2> <p></font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> Main(</font><font color="#0000ff" size=2>string</font><font size=2>[] args)</p> <p>{</p> <p></font><font color="#008080" size=2>FileStream</font><font size=2> exclusiveWriter = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>FileStream</font><font size=2>(</font><font color="#800000" size=2>@&quot;C:\Temp\FileLockTest1.txt&quot;</font><font size=2>, </font><font color="#008080" size=2>FileMode</font><font size=2>.OpenOrCreate, </font><font color="#008080" size=2>FileAccess</font><font size=2>.Write, </font><font color="#008080" size=2>FileShare</font><font size=2>.None);</p> <p></font><font color="#0000ff" size=2>for</font><font size=2> (</font><font color="#0000ff" size=2>byte</font><font size=2> counter = 0; counter &lt; 100; counter++)</p> <p>{</p> <p>exclusiveWriter.WriteByte(counter);</p> <p>}</p> <p></font><font color="#008080" size=2>FileStream</font><font size=2> sharedReader = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>FileStream</font><font size=2>(</font><font color="#800000" size=2>@&quot;C:\Temp\FileLockTest1.txt&quot;</font><font size=2>, </font><font color="#008080" size=2>FileMode</font><font size=2>.Open, </font><font color="#008080" size=2>FileAccess</font><font size=2>.Read, </font><font color="#ff0000" size=2><strong>FileShare</strong></font><font size=2><strong><font color="#ff0000">.Read</font></strong>);</p> <p>sharedReader.Seek(0, </font><font color="#008080" size=2>SeekOrigin</font><font size=2>.Begin);</p> <p></font><font color="#0000ff" size=2>for</font><font size=2> (</font><font color="#0000ff" size=2>byte</font><font size=2> counter = 0; counter &lt; 100; counter++)</p> <p>{</p> <p></font><font color="#008080" size=2>Console</font><font size=2>.WriteLine(sharedReader.ReadByte());</p> <p>}</p> <p>exclusiveWriter.Close();</p> <p>sharedReader.Close();</p> <p></font><font color="#008080" size=2>Console</font><font size=2>.ReadLine();</p> <p>}</p> <p> </p> <p align=left>Thanks in advance,</p> <p align=left>- Johnson</font></p> <p></p></td></tr></tbody></table></td></tr></tbody></table></div>Try to change <font color="#ff0000"><strong>FileShare</strong></font><font size=2><font color="#ff0000"><strong>.Read</strong></font> to <font color="#2b91af" size=2>FileShare</font><font size=2>.ReadWrite .</font></font> <p></p> <p align=left> </p> <p align=left>Best Regards.</p>Fri, 02 May 2008 07:12:37 Z2008-05-02T07:12:37Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#452b462a-3ba2-4114-937f-c86002c2aa7fhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#452b462a-3ba2-4114-937f-c86002c2aa7fJohn DeSouzahttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20DeSouzaHow to read files that are exclusively locked by other applications?<p align=left><font face=Arial size=2></font> </p> <p>Hi,</p> <p align=left>Thanks for your reply. I tried changing the FileShare to &quot;ReadWrite&quot;. But am still getting the same exception!!</p> <p align=left> </p>Fri, 02 May 2008 08:23:13 Z2008-05-02T08:23:13Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#03e416cf-c497-495c-be05-c4fd26670d6bhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#03e416cf-c497-495c-be05-c4fd26670d6bSasha Goldshteinhttp://social.msdn.microsoft.com/Profile/en-US/?user=Sasha%20GoldshteinHow to read files that are exclusively locked by other applications?Of course you are getting the same exception.  If a process opens a file and specifies FileShare.None (which is equivalent to FILE_SHARE_NONE in Win32), then there is no documented way to access the file from another process.  That's the whole idea of the file share enumeration.Fri, 02 May 2008 08:49:14 Z2008-05-02T08:49:14Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#0aec7219-dc0c-4490-b75f-724c21b58e89http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#0aec7219-dc0c-4490-b75f-724c21b58e89John DeSouzahttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20DeSouzaHow to read files that are exclusively locked by other applications?<p>This is an important business requirement and I've got to implement it anyhow. Is there any &quot;undocumented&quot; way or any workaround to get this done?</p> <p align=left>Currently I copy the file to a temp location and read it from there. Not the most elegant solution I know. Any help would be appreciated.</p> <p align=left> </p> <p align=left>Thanks in advance.</p> <p align=left> </p> <p align=left><font face=Arial size=2></font> </p>Fri, 02 May 2008 10:08:49 Z2008-05-02T10:08:49Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#c9035641-71b7-4472-a62a-cacd35eb26c6http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#c9035641-71b7-4472-a62a-cacd35eb26c6ShellShockhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShellShockHow to read files that are exclusively locked by other applications?<p align=left><font face=Arial size=2>I can only see two options:</font></p> <p align=left> </p> <p align=left>1 Get the daemon process code changed so it allows read access. This assumes you have access to the source code, or you can get it changed. Why do people write logging code that exclusive locks the log file? This is very bad practice!<img src="http://forums.microsoft.com/MSDN/WebResource.axd?d=NySzF1eivP_rMoc50GQJzcvS4MHMOEKwYrCIgDtzuzlw7GsNki3H_INlfYaLgkxFSSG0MZbJ1z6uFFjZvu3APg2&amp;t=633337194230757564"> Maybe they don't want anyone to ever look at the log?</p> <p align=left> </p> <p align=left>2. Copy the file as you are already doing.</p>Fri, 02 May 2008 16:34:02 Z2008-05-05T06:45:38Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#55079074-0f9f-4f51-8481-1e817ebeaa7bhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#55079074-0f9f-4f51-8481-1e817ebeaa7bEyalShilonyhttp://social.msdn.microsoft.com/Profile/en-US/?user=EyalShilonyHow to read files that are exclusively locked by other applications?<div class=quote> <table width="85%"> <tbody> <tr> <td class=txt4> <strong>ShellShock wrote:</strong></td></tr> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%"> <p align=left><font face=Arial size=2>I can only see two options:</font></p> <p align=left> </p> <p align=left>1 Get the daemon process code changed so it allows read access. This assumes you have access to the source code, or you can get it changed. Why do people write logging code that exclusive locks the log file? This is very bad practice!<img src="http://forums.microsoft.com/MSDN/WebResource.axd?d=NySzF1eivP_rMoc50GQJzcvS4MHMOEKwYrCIgDtzuzlw7GsNki3H_INlfYaLgkxFSSG0MZbJ1z6uFFjZvu3APg2&amp;t=633337194230757564"> Maybe they don't want anyone to ever look at the log?</p> <p align=left> </p> <p align=left>2. Copy the file as you are already doing.</p> <p></p></td></tr></tbody></table></td></tr></tbody></table></div> <p></p> <p align=left> </p> <p align=left>1, Not really a bad practice, it might be a requirement to one of the process operations.</p> <p align=left> </p> <p align=left>2. Not the most elegant solution as the OP said but it should work. <img alt=Smile src="http://forums.microsoft.com/MSDN/emoticons/emotion-1.gif"></p>Sat, 03 May 2008 22:25:15 Z2008-05-03T22:25:15Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#1badbf81-2fe3-4f5f-8c53-901db6e9f8afhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#1badbf81-2fe3-4f5f-8c53-901db6e9f8afJohn DeSouzahttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20DeSouzaHow to read files that are exclusively locked by other applications?<p>Hmm.. I have done it by copying the file locally and works fine for now.</p> <p align=left>Thank you all for your time.</p> <p align=left><font face=Arial size=2></font> </p>Fri, 09 May 2008 08:29:44 Z2008-05-09T08:29:44Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#50a92173-5652-4083-b07b-5ea64ad45c54http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/26f31d41-c1ad-4fa0-b00e-e891a7ab4739#50a92173-5652-4083-b07b-5ea64ad45c54John DeSouzahttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20DeSouzaHow to read files that are exclusively locked by other applications?<p>Hmm.. I have done it by copying the file locally and works fine for now.</p> <p align=left>Thank you all for your time. </p> <p align=left><font face=Arial size=2></font> </p>Fri, 09 May 2008 08:29:58 Z2008-05-09T08:29:58Z