<?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>out &#62;&#62; m_Conscientia; &#187; Isolated Storage</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/isolated-storage/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Sat, 27 Aug 2011 19:16:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Isolated Storage</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/isolated-storage/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/isolated-storage/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 19:08:04 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[Isolated Storage]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=254</guid>
		<description><![CDATA[Isolated storage is provided so that applications running with least privilege are able to store data. .NET provides isolated storage equivalents for both File and FileStream. IsolatedStorageFile IsolatedStorageFile provides functionality to create files and folders in isolated storage. The first step is to create a store, which can be scoped as either Assembly/Machine or Assembly/User. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/3ak841sy.aspx"><br />
Isolated storage</a> is provided so that applications running with least privilege are able to store data. .NET provides isolated storage equivalents for both File and FileStream.</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx">IsolatedStorageFile</a> </strong></p>
<p>IsolatedStorageFile provides functionality to create files and folders in isolated storage. The first step is to create a store, which can be scoped as either Assembly/Machine or Assembly/User. If you use machine scope, then all users can access the data. So, user specific data should go in user scope, application data in machine scope.</p>

<div class="wp_codebox"><table><tr id="p2545"><td class="code" id="p254code5"><pre class="csharp" style="font-family:monospace;">IsolatedStorageFile machineStore <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMachineStoreForAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
IsolatedStorageFile userStore <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserStoreForAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Click once applications can also access an <a href="http://blogs.msdn.com/shawnfa/archive/2006/01/18/514407.aspx">application scope</a>.</p>
<p>Once you have created a store you can use instance methods to manipulate files and directories. Instance properties allow you to evaluate current and maximum size, scope, and the identities (Application, Assembly, Domain) used for scoping.</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.aspx">IsolatedStorageFileStream</a></strong></p>
<p>The IsolatedStorageFileStream class derives from FileStream, see <a href="http://blog.hypercomplex.co.uk/index.php/2009/07/net-streams/">.NET streams</a> for more details. Here is an example of writing within a directory:</p>

<div class="wp_codebox"><table><tr id="p2546"><td class="code" id="p254code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFile userStore <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserStoreForAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    userStore<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateDirectory</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;testDir&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFileStream userStream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> IsolatedStorageFileStream<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;testDir\user.set&quot;</span>, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenOrCreate</span>, userStore<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>StreamWriter userWriter <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamWriter<span style="color: #008000;">&#40;</span>userStream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            userWriter<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;user data&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s slightly annoying that there is no directory exists method, so testing this before creating a directory involves calling GetDirectoryNames then iterating over the string array it returns &#8211; I didn&#8217;t show this for brevity. Here is how to read it back:</p>

<div class="wp_codebox"><table><tr id="p2547"><td class="code" id="p254code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFile userStore <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserStoreForAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFileStream userStream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> IsolatedStorageFileStream<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;testDir\user.set&quot;</span>, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span>, userStore<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        StreamReader userReader <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span>userStream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> userReader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Permissions</strong></p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.security.permissions.isolatedstoragepermission.aspx">IsolatedStoragePermission</a> class defines access to isolated storage capabilities. If you use isolated storage it is best practice to add an IsolatedStoragePermission attribute.</p>

<div class="wp_codebox"><table><tr id="p2548"><td class="code" id="p254code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>IsolatedStorageFilePermission<span style="color: #008000;">&#40;</span>SecurityAction<span style="color: #008000;">.</span><span style="color: #0000FF;">Demand</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* ... */</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/isolated-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

