<?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>Dot Net 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>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.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 us. And I don’t think there is a way to avoid this option available in [...]]]></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;">
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;">
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>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
