<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How To User StreamReader to Open A File ReadOnly in C# (.NET)</title>
	<atom:link href="http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=csharp-streamreader-readonly</link>
	<description>Microsoft Focused, JavaScript,HTML5 (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Tue, 15 May 2012 21:53:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Rajesh</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35706</link>
		<dc:creator>Rajesh</dc:creator>
		<pubDate>Mon, 02 Apr 2012 11:07:10 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35706</guid>
		<description>Bob,

I managed to get my requirement coded - which was to read from a file that was continuously getting updated by other process. I think you are looking for similar one... Here&#039;s what I did.

FileStream fs = new FileStream(&quot;myfile.txt&quot;, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader streamReader = new StreamReader(fs);
string line;
  while ((line = streamReader.ReadLine()) != null)
  {  
       // do something      
   }

The FileShare.ReadWrite in fs object is important to let your program know that the file is being updated by another process. This way it reads the new contents that are written while it was already opened for reading. Hope this helps!

Cheers.</description>
		<content:encoded><![CDATA[<p>Bob,</p>
<p>I managed to get my requirement coded &#8211; which was to read from a file that was continuously getting updated by other process. I think you are looking for similar one&#8230; Here&#8217;s what I did.</p>
<p>FileStream fs = new FileStream(&#8220;myfile.txt&#8221;, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);<br />
StreamReader streamReader = new StreamReader(fs);<br />
string line;<br />
  while ((line = streamReader.ReadLine()) != null)<br />
  {<br />
       // do something<br />
   }</p>
<p>The FileShare.ReadWrite in fs object is important to let your program know that the file is being updated by another process. This way it reads the new contents that are written while it was already opened for reading. Hope this helps!</p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rajesh</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35705</link>
		<dc:creator>Rajesh</dc:creator>
		<pubDate>Mon, 02 Apr 2012 09:36:04 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35705</guid>
		<description>The code looks good to me, but When I manually opened a file and edited it but didn&#039;t save it yet!, Then ran my program with the above code to open it in ReadOnly mode and read the stream. While it is reading the stream, I tried to save the file which I manually updated earlier. This doesn&#039;t allow me to save it saying there is a Sharing violation and cannot save the file. Any ideas why this issue arises even when the file is being opened in File.OpenRead(file) method.</description>
		<content:encoded><![CDATA[<p>The code looks good to me, but When I manually opened a file and edited it but didn&#8217;t save it yet!, Then ran my program with the above code to open it in ReadOnly mode and read the stream. While it is reading the stream, I tried to save the file which I manually updated earlier. This doesn&#8217;t allow me to save it saying there is a Sharing violation and cannot save the file. Any ideas why this issue arises even when the file is being opened in File.OpenRead(file) method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Kellner</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35349</link>
		<dc:creator>Peter Kellner</dc:creator>
		<pubDate>Mon, 05 Mar 2012 16:34:20 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35349</guid>
		<description>hmm.  It looks to me like OpenRead is there, all the way back to .net 2.0   http://msdn.microsoft.com/en-US/library/system.io.file_methods(v=vs.80).aspx</description>
		<content:encoded><![CDATA[<p>hmm.  It looks to me like OpenRead is there, all the way back to .net 2.0   <a href="http://msdn.microsoft.com/en-US/library/system.io.file_methods(v=vs.80)">http://msdn.microsoft.com/en-US/library/system.io.file_methods(v=vs.80)</a>.aspx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35348</link>
		<dc:creator>Bob</dc:creator>
		<pubDate>Mon, 05 Mar 2012 16:18:23 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-35348</guid>
		<description>Ben, that still gives me an error that the file is in use by another process, even though I can open it in Windows.</description>
		<content:encoded><![CDATA[<p>Ben, that still gives me an error that the file is in use by another process, even though I can open it in Windows.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-30998</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Thu, 06 Oct 2011 21:46:28 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-30998</guid>
		<description>I beleive that you want to do something like this

using( FileStream s = File.OpenRead(filename))
{
   using(TextReader reader = new StreamReader(s))
   {
     // Do smoething
   }
}

This ensures the both streams get closed properly.

Cheers
Ben</description>
		<content:encoded><![CDATA[<p>I beleive that you want to do something like this</p>
<p>using( FileStream s = File.OpenRead(filename))<br />
{<br />
   using(TextReader reader = new StreamReader(s))<br />
   {<br />
     // Do smoething<br />
   }<br />
}</p>
<p>This ensures the both streams get closed properly.</p>
<p>Cheers<br />
Ben</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MarkJoel60</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-30407</link>
		<dc:creator>MarkJoel60</dc:creator>
		<pubDate>Wed, 17 Aug 2011 19:47:18 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-30407</guid>
		<description>@Ash: I believe you are incorrect. I am using VS2010, and this works:


System.IO.StreamReader streamReader = new              System.IO.StreamReader(System.IO.File.OpenRead(&quot;myfile.txt&quot;));</description>
		<content:encoded><![CDATA[<p>@Ash: I believe you are incorrect. I am using VS2010, and this works:</p>
<p>System.IO.StreamReader streamReader = new              System.IO.StreamReader(System.IO.File.OpenRead(&#8220;myfile.txt&#8221;));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ash</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-27822</link>
		<dc:creator>Ash</dc:creator>
		<pubDate>Fri, 23 Jul 2010 18:27:44 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-27822</guid>
		<description>File.OpenRead(file) .. doesn&#039;t seem to work in VS 2010 C#. The only option (in Intellisense) I get is file.OpenFile no file.openRead. Any thoughts?</description>
		<content:encoded><![CDATA[<p>File.OpenRead(file) .. doesn&#8217;t seem to work in VS 2010 C#. The only option (in Intellisense) I get is file.OpenFile no file.openRead. Any thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-25868</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Tue, 08 Dec 2009 20:06:55 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-25868</guid>
		<description>Hi Ryan,
Sorry, don&#039;t know about performance, but I&#039;d guess opening in ReadOnly has not perf impact.
-Peter</description>
		<content:encoded><![CDATA[<p>Hi Ryan,<br />
Sorry, don&#8217;t know about performance, but I&#8217;d guess opening in ReadOnly has not perf impact.<br />
-Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-25867</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Tue, 08 Dec 2009 20:06:03 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-25867</guid>
		<description>Nice, I did not know of that static method.

Do you know if it improves performance/memory if opening a file for reading purposes only vs. opening it in RW mode?</description>
		<content:encoded><![CDATA[<p>Nice, I did not know of that static method.</p>
<p>Do you know if it improves performance/memory if opening a file for reading purposes only vs. opening it in RW mode?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bartek</title>
		<link>http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-25862</link>
		<dc:creator>Bartek</dc:creator>
		<pubDate>Tue, 08 Dec 2009 12:03:21 +0000</pubDate>
		<guid isPermaLink="false">http://peterkellner.net/2009/12/07/csharp-streamreader-readonly/#comment-25862</guid>
		<description>Thanks Peter, I was just looking for something like this!</description>
		<content:encoded><![CDATA[<p>Thanks Peter, I was just looking for something like this!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 46/48 queries in 0.011 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: PetersBlogCDN.s3.amazonaws.com

Served from: peterkellner.net @ 2012-05-21 11:58:47 -->
