<p>I'm working on a WinCE 6.0 system with a touchscreen that stores its calibration data (x-y location, offset, etc.) in the system registry (HKLM\HARDWARE\TOUCH).  Right now, I'm placing the cal values into registry keys that get put into the OS image at build time.  That works fine for the monitor that I get the original cal values from, but when I load this image into another system with a different monitor, the touchscreen pointer location is (understandably) off, because the two monitors do not have the same cal values.</p>

<p>My problem is that I don't know how to properly store values into the registry so that they persist after a power cycle.  See, I can recalibrate the screen on the second system, but the new values only exist in volatile memory.  I suggested to my boss that we could just tell our customer to leave the power on the unit at all times -- that didn't go over well.</p>

<p>I need advice on how to save the new constants into the registry, so that we can calibrate the monitors once before shipping them out to our customer, and not have to make separate OS images for each unit we build.</p>

<p>A C# method that is known to work in CE6.0 would be helpful.  Thanks.</p>

<p>-Odbasta</p>

[{'answer_id': 57965, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 3, 'selected': True, 'text': '<p>I think what you\'re probably looking for is the Flush function of the RegistryKey class. This is normally not necessary (the registry is lazily-flushed by default), but if the power is turned off on the device before the system has a chance to do this, changes will be discarded:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.flush.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.flush.aspx</a></p>\n\n<p>This function is available in .NET Compact Framework version 2.0 and better.</p>\n'}, {'answer_id': 59995, 'author': 'odbasta', 'author_id': 2488, 'author_profile': 'https://Stackoverflow.com/users/2488', 'pm_score': 3, 'selected': False, 'text': '<p>Follow-up on this question:</p>\n\n<p>Thanks DannySmurf, flushing the registry key was ultimately what needed to be done.  However, there were a few steps that I was missing before reaching that stage.  So, here\'s what came to light:</p>\n\n<ul>\n<li>I was using a RAM-based registry, where by design the registry does not persist after a cold boot.  I had to switch the registry to hive-based.</li>\n<li><p>When switching to a hive-based registry structure, you need to make sure that the hive exists on a non-volatile medium.  This is specified in the platform.reg file:</p>\n\n<pre><code>[HKEY_LOCAL_MACHINE\\init\\BootVars]\n"SystemHive"="\\\\Hard Disk\\\\system.hv"\n"ProfileDir"="\\\\Documents and Settings"\n"RegistryFlags"=dword:1               ; Flush hive on every RegCloseKey call\n"SystemHiveInitialSize"=dword:19000   ; Initial size for hive-registry file \n"Start DevMgr"=dword:1\n</code></pre></li>\n<li><p>Once the system.hv file is on the hard disk (CF card in my case), the values in the registry will persist after a cold boot.  Note that the system.hv file contains all the HKLM keys.</p></li>\n<li><p>It\'s also important to note that any drivers that need to be initialized on boot have to be specified as such in the .reg files of the solution.  For example, I had to make sure that the hard disk drivers (PCMCIA) were loaded before trying to read the system hive file from them.  The way to do this is to add a directive in the following format around each driver init key:</p>\n\n<pre><code>;HIVE BOOT SECTION\n[HKEY_LOCAL_MACHINE\\Drivers\\PCCARD\\PCMCIA\\TEMPLATE\\PCMCIA]\n  "Dll"="pcmcia.dll"\n  "NoConfig"=dword:1\n  "IClass"=multi_sz:"{6BEAB08A-8914-42fd-B33F-61968B9AAB32}=PCMCIA Card Services"\n  "Flags"=dword:1000\n;END HIVE BOOT SECTION\n</code></pre></li>\n</ul>\n\n<p>That, plus a lot of luck, is about it.</p>\n'}, {'answer_id': 66608, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>As I understood you need to know how to set a value to the registry during runtime. I hope the codes bellow can help you.</p>\n\n<p>using Microsoft.Win32;       </p>\n\n<pre><code>    /// &lt;summary&gt;\n    /// store a key value in registry. if it don\'t exist it will be created. \n    /// &lt;/summary&gt;\n    /// &lt;param name="mainKey"&gt;the main key of key path&lt;/param&gt;\n    /// &lt;param name="subKey"&gt;the path below the main key&lt;/param&gt;\n    /// &lt;param name="keyName"&gt;the key name&lt;/param&gt;\n    /// &lt;param name="value"&gt;the value to be stored&lt;/param&gt;\n    public static void SetRegistry(int mainKey, String subKey, String keyName, object value)\n    {\n        if (mainKey != CURRENT_USER &amp;&amp; mainKey != LOCAL_MACHINE)\n        {\n            throw new ArgumentOutOfRangeException("mainKey", "\\\'mainKey\\\' argument can only be AppUtils.CURRENT_USER or AppUtils.LOCAL_MACHINE values");\n        }\n\n        if (subKey == null)\n        {\n            throw new ArgumentNullException("subKey", "\\\'subKey\\\' argument cannot be null");\n        }\n\n        if (keyName == null)\n        {\n            throw new ArgumentNullException("keyName", "\\\'keyName\\\' argument cannot be null");\n        }\n\n        const Boolean WRITABLE = true;\n        RegistryKey key = null;\n\n        if (mainKey == CURRENT_USER)\n        {\n            key = Registry.CurrentUser.OpenSubKey(subKey, WRITABLE);\n\n            if (key == null)\n            {\n                key = Registry.CurrentUser.CreateSubKey(subKey);\n            }\n        }\n        else if (mainKey == LOCAL_MACHINE)\n        {\n            key = Registry.LocalMachine.OpenSubKey(subKey, WRITABLE);\n\n            if (key == null)\n            {\n                key = Registry.LocalMachine.CreateSubKey(subKey);\n            }\n        }\n\n        key.SetValue(keyName, value);\n\n    }\n\n    /// &lt;summary&gt;\n    /// find a key value in registry. if it don\'t exist the default value will be returned.\n    /// &lt;/summary&gt;\n    /// &lt;param name="mainKey"&gt;the main key of key path&lt;/param&gt;\n    /// &lt;param name="subKey"&gt;the path below the main key&lt;/param&gt;\n    /// &lt;param name="keyName"&gt;the key name&lt;/param&gt;\n    /// &lt;param name="defaultValue"&gt;the value to be stored&lt;/param&gt;\n\n    public static object GetRegistry(int mainKey, String subKey, String keyName, object defaultValue)\n    {\n        if (mainKey != CURRENT_USER &amp;&amp; mainKey != LOCAL_MACHINE)\n        {\n            throw new ArgumentOutOfRangeException("mainKey", "\\\'mainKey\\\' argument can only be AppUtils.CURRENT_USER or AppUtils.LOCAL_MACHINE values");\n        }\n\n        if (subKey == null)\n        {\n            throw new ArgumentNullException("subKey", "\\\'subKey\\\' argument cannot be null");\n        }\n\n        if (keyName == null)\n        {\n            throw new ArgumentNullException("keyName", "\\\'keyName\\\' argument cannot be null");\n        }\n\n        RegistryKey key = Registry.CurrentUser.OpenSubKey(subKey);\n\n        if (mainKey == CURRENT_USER)\n        {\n            key = Registry.CurrentUser.OpenSubKey(subKey);\n        }\n        else if (mainKey == LOCAL_MACHINE)\n        {\n            key = Registry.LocalMachine.OpenSubKey(subKey);\n        }\n\n        object result = defaultValue;\n\n        if (key != null)\n        {\n            result = key.GetValue(keyName, defaultValue);\n        }\n\n        return result;\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/57609', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2488/']