<?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>Visible Invisibles &#187; C#</title>
	<atom:link href="http://munashiku.slightofmind.net/category/computers/programming/csharp/feed" rel="self" type="application/rss+xml" />
	<link>http://munashiku.slightofmind.net</link>
	<description>Revealing the Opacity of Transparency</description>
	<lastBuildDate>Tue, 02 Aug 2011 09:15:56 +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>Storing Program Settings Using the Registry</title>
		<link>http://munashiku.slightofmind.net/20090822/storing-program-settings-using-the-registry</link>
		<comments>http://munashiku.slightofmind.net/20090822/storing-program-settings-using-the-registry#comments</comments>
		<pubDate>Sat, 22 Aug 2009 11:40:25 +0000</pubDate>
		<dc:creator>Whesley McCabe</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://munashiku.slightofmind.net/?p=1178</guid>
		<description><![CDATA[Many programs use the registry to store persistent data such as settings or user defined variables &#8211; which are really just settings, too &#8211; which can be retrieved whenever the program needs them. These settings survive, in most cases,  multiple loadings and unloadings of the program, uninstallation of the program, reinstallation of the program, update [...]]]></description>
			<content:encoded><![CDATA[<p>Many programs use the registry to store persistent data such as settings or user defined variables &#8211; which are really just settings, too &#8211; which can be retrieved whenever the program needs them. These settings survive, in most cases,  multiple loadings and unloadings of the program, uninstallation of the program, reinstallation of the program, update or upgrading of the program, and program crashes. This makes the registry a very valuable place to store these settings!</p>
<p>If you&#8217;re creating your own program, maybe you&#8217;ve already considered how you were going to save the settings. Well, saving your program&#8217;s settings to the registry is a piece of cake, and I&#8217;m going to show you how. <span id="more-1178"></span></p>
<p>The following code will allow you to store and retrieve settings from the registry:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Win32</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> ProgramName<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// LocalSettings will allow you to access the registry</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> LocalSettings<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This path is important, as it will be the new home for your</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// program's settings. Registry.CurrentUser is HKCU.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// It is recommended to limit settings to the HKCU.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// DON'T FORGET TO CHANGE {YOUR PROGRAM NAME HERE}</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> PATH <span style="color: #008000;">=</span> Registry<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUser</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>SOFTWARE<span style="color: #008080; font-weight: bold;">\\</span>{YOUR PROGRAM NAME HERE}<span style="color: #008080; font-weight: bold;">\\</span>&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This will save all keys to the registry, regardless of type.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> saveKey<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span> keyName, <span style="color: #6666cc; font-weight: bold;">object</span> keyVal<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Registry<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>PATH, keyName, keyVal<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This will load any key that has been stored as a string.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This will return the loaded key's value as a string.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> loadKeyString<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span> keyName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">object</span> keyVal <span style="color: #008000;">=</span> Registry<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>PATH, keyName, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyVal <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>keyVal<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This will load any key that has been stored as boolean.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This will return the loaded key's value as true or false.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> loadKeyBool<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span> keyName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">object</span> keyVal <span style="color: #008000;">=</span> Registry<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>PATH, keyName, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>keyVal <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>keyVal<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>This method of storing and retrieving settings is only useful for saving individual keys and values, so if your program needs to save and load a lot of settings, you may wish to explore other options. To save a key and value to the registry, you would use <code class="codecolorer csharp default"><span class="csharp">LocalSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">saveKey</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#123;</span>NAME<span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span>VALUE<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></span></code> where <strong>{NAME}</strong> is the Key name and <strong>{VALUE}</strong> is the value to be saved. The value can be a string or true/false stored as a string with a value of &#8220;true&#8221; or &#8220;false&#8221;. To retrieve a saved value of a stored string, use <code class="codecolorer csharp default"><span class="csharp"><span style="color: #008000;">&#123;</span>X<span style="color: #008000;">&#125;</span> <span style="color: #008000;">=</span> loadKeyString<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{NAME}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></span></code> where <strong>{NAME}</strong> is the name of the key you are getting the value of, and <strong>{X}</strong> is any object you want to hold the value you are retrieving. If you are retrieving a stored boolean value, use <code class="codecolorer csharp default"><span class="csharp"><span style="color: #008000;">&#123;</span>Z<span style="color: #008000;">&#125;</span> <span style="color: #008000;">=</span> loadKeyBool<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{NAME}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></span></code> where <strong>{NAME}</strong> is the name of the key you are getting the value of, and <strong>{Z}</strong> is any object you want to hold the value you are retrieving. Keep in mind, when using variables to store the values you just retrieved, the variable <strong>{X}</strong> above must be created as <code class="codecolorer csharp default"><span class="csharp"><span style="color: #6666cc; font-weight: bold;">string</span> <span style="color: #008000;">&#123;</span>X<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></span></code> since the value of the key is a string, and the variable <strong>{Z}</strong> above must be created as <code class="codecolorer csharp default"><span class="csharp"><span style="color: #6666cc; font-weight: bold;">bool</span> <span style="color: #008000;">&#123;</span>Z<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></span></code> since the value of the key is <strong><em>true</em></strong> or <strong><em>false</em></strong>.</p>
<p>With any luck I have been at least <em>some</em> help to someone, or at the very least I&#8217;ve hopefully inspired someone &#8211; maybe you &#8211; to find a better way to access the registry and let others know how to do it!</p>
]]></content:encoded>
			<wfw:commentRss>http://munashiku.slightofmind.net/20090822/storing-program-settings-using-the-registry/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adventures in C#</title>
		<link>http://munashiku.slightofmind.net/20090816/adventures-in-csharp</link>
		<comments>http://munashiku.slightofmind.net/20090816/adventures-in-csharp#comments</comments>
		<pubDate>Sun, 16 Aug 2009 05:25:23 +0000</pubDate>
		<dc:creator>Whesley McCabe</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PocketPress]]></category>
		<category><![CDATA[Visual Studio 2008]]></category>

		<guid isPermaLink="false">http://munashiku.slightofmind.net/?p=1145</guid>
		<description><![CDATA[I&#8217;ve been really lax in making some posts here, and the main reason is that I didn&#8217;t have anything to share. Considering the new project that I have undertaken (see PocketPress), I think I have plenty to share now. Thus begins my series of posts about my adventures in programming with C# in Visual Studio [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been really lax in making some posts here, and the main reason is that I didn&#8217;t  have anything to share. Considering the new project that I have undertaken (see <a href="http://munashiku.slightofmind.net/20090811/new-project-pocketpress">PocketPress</a>), I think I have plenty to share now. Thus begins my series of posts about my adventures in programming with C# in Visual Studio 2008.</p>
<p>I put together a few test programs in various languages, mainly C++, C# and VB, as well as PPL and Java, before finally deciding on C#.  By far, the easiest for me to program would be VB, since I&#8217;ve already done a fair bit of VB programming, and C++ would be a close second. PPL and Java felt like more effort than it was worth, considering I have no experience in either and I can get the same thing done with less effort in any other language. I finally decided on C# for its easy form designing, like VB.NET, and familiar programming flow, like C++.</p>
<p>I still have a lot to learn about programming in C#, but it doesn&#8217;t feel like it&#8217;s going to be very difficult. I guess we&#8217;ll see!</p>
]]></content:encoded>
			<wfw:commentRss>http://munashiku.slightofmind.net/20090816/adventures-in-csharp/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

