Writing and Reading Windows Registry values in Linux and Windows
This a small plugin to test reading and writing Registry values using the same code for Windows and Linux
Previously I had used the VB methods of reading and writing to the Registry;
using Microsoft.VisualBasic;
public static string Interaction.GetSetting(
string AppName,
string Section,
string Key,
string Default = ""
)
public static void Interaction.SaveSetting(
string AppName,
string Section,
string Key,
string Setting
)
But they don't work with Linux so I decided to write two replacement methods that made it easy to adapt my existing code, here they are;
using Microsoft.Win32;
public string GetRegSetting(
string AppName,
string Section,
string Key,
string Default
)
public bool SaveRegSetting(
string AppName,
string Section,
string Key,
string Setting
)
In GetRegSetting the Default parameter must be at the very least a zero length string, ""
If values are not found in the Registry then the Default value is returned.
The following code shows how to use them.
I've tested it with Windows and Linux and works okay with both.

using System;
using System.Windows.Forms;
using CamBam.CAD;
using CamBam.UI;
using Microsoft.Win32;
namespace MyNamespace
{
public class Class1
{
public static void InitPlugin(CamBamUI ui)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "REG Save";
item.Click += new EventHandler(plugin_clicked);
ui.Menus.mnuPlugins.DropDownItems.Add(item);
ToolStripMenuItem item2 = new ToolStripMenuItem();
item2.Text = "REG Get";
item2.Click += new EventHandler(plugin_clicked2);
ui.Menus.mnuPlugins.DropDownItems.Add(item2);
}
public static void plugin_clicked(object sender, EventArgs e)
{
Myplugin mp1 = new Myplugin();
mp1.SetRegistryValues();
}
public static void plugin_clicked2(object sender, EventArgs e)
{
Myplugin mp1 = new Myplugin();
mp1.GetRegistryValues();
}
public class Myplugin
{
public void SetRegistryValues()
{
SaveRegSetting("REGtest", "Settings", "diameter", "12");
SaveRegSetting("REGtest", "Settings", "Tip", "Ball Nose");
SaveRegSetting("REGtest", "Settings", "Speed", "20000");
}
public void GetRegistryValues()
{
string val1 = "";
string val2 = "";
string val3 = "";
// NOTE: at the very least Default must be a zero length string, ""
val1 = GetRegSetting("REGtest", "Settings", "diameter", "");
val2 = GetRegSetting("REGtest", "Settings", "Tip", "V");
val3 = GetRegSetting("REGtest", "Settings", "Speed", "10000");
CamBam.ThisApplication.AddLogMessage("val1 = " + val1);
CamBam.ThisApplication.AddLogMessage("val2 = " + val2);
CamBam.ThisApplication.AddLogMessage("val3 = " + val3);
}
public string GetRegSetting(string AppName, string Section, string Key, string Default)
{
// attempt to open the subkey
RegistryKey regKey = Registry.CurrentUser.OpenSubKey("Software\\" + AppName + "\\" + Section, false);
// if it fails then return Default value
if (regKey == null)
{
return Default;
}
else
{
try
{
object setting = regKey.GetValue(Key);
if (setting == null) return Default;
return setting.ToString();
}
catch (Exception e)
{
CamBam.ThisApplication.AddLogMessage("Registry Read Error " + e);
return null;
}
}
}
public bool SaveRegSetting(string AppName, string Section, string Key, string Setting)
{
try
{
// Use CreateSubKey (create or open it if already exits)
// located in, HKEY_CURRENT_USER\Software
RegistryKey rootKey = Registry.CurrentUser.CreateSubKey("Software");
RegistryKey appKey = rootKey.CreateSubKey(AppName);
RegistryKey sectionKey = appKey.CreateSubKey(Section);
sectionKey.SetValue(Key, Setting);
return true;
}
catch (Exception e)
{
CamBam.ThisApplication.AddLogMessage("Registry Write Error " + e);
return false;
}
}
}
}
}