<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dotnet thoughts &#187; NoCache</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/nocache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Writing File with Non Cache mode in C#</title>
		<link>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/</link>
		<comments>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:39:16 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FileStream]]></category>
		<category><![CDATA[NoCache]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=733</guid>
		<description><![CDATA[Normally when we are writing to the File Systems or I/O devices, Windows will cache the request or response to get better performance. This behavior is a good feature, but sometimes we require immediate change. By caching Windows sometimes mislead &#8230; <a href="http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Normally when we are writing to the File Systems or I/O devices, Windows will cache the request or response to get better performance. This behavior is a good feature, but sometimes we require immediate change. By caching Windows sometimes mislead us. And I don’t think there is a way to avoid this option available in Windows. In my current project I got a chance to explore / work on this, but I need to avoid this caching. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I checked various options with File class and Stream class but there was no option available to avoid Caching. Later our VC++ developer gives me some code, which in WIN32 API, which will avoid caching. It was using &#8220;<em>CreateFile()</em>&#8221; method in Kernal32.dll with <em>FILE_FLAG_NO_BUFFERING</em> option. Then I was able to create same in C# code base on the input using PInvoke. But I have to find a managed code, with that we can write / read stream without caching. Later I found FileStream class, which supporting both synchronous and asynchronous read and write operations. But it also doesn’t have a NonCahce file option. Then I tried FileStream class with <em>FILE_FLAG_NO_BUFFERING</em> option. And it worked <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="brush: csharp; title: ; notranslate">
const FileOptions FILE_FLAG_NO_BUFFERING = (FileOptions) 0x20000000;

using(FileStream fs = new FileStream(&quot;Path&quot;,FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.WriteThrough | FILE_FLAG_NO_BUFFERING))
{
fs.Write(&quot;HelloWorld&quot;);
}
</pre>
<p>This FileStream class also got a nice option, <em>FileOptions.DeleteOnClose</em>, if you are creating a File with this option enabled, it will delete the File after you close the FileStream. This can be used for creating real temporary files.</p>
<pre class="brush: csharp; title: ; notranslate">
string TempFile = Path.GetTempFileName();
using(FileStream fs = new FileStream(TempFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.DeleteOnClose))
{
fs.Write(&quot;HelloWorld&quot;);
} //Deletes the File.
</pre>
<p>I think this FileStream class is available from .Net 2.0 Framework onwards.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2008/05/08/using-iocompression-namespace/" title="Permanent link to Using IO.Compression namespace">Using IO.Compression namespace</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/09/22/adding-and-reading-files-from-sql-server-2008-filestream/" title="Permanent link to Adding and Reading files from SQL Server 2008 Filestream">Adding and Reading files from SQL Server 2008 Filestream</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/02/03/isolated-storage-in-c/" title="Permanent link to Isolated Storage in C#">Isolated Storage in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/26/how-to-upload-file-using-httpwebrequest-class/" title="Permanent link to How to upload file using HttpWebRequest class">How to upload file using HttpWebRequest class</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/06/filestream-in-sql-server-2008/" title="Permanent link to FILESTREAM in SQL Server 2008">FILESTREAM in SQL Server 2008</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

