Thanks to Sreekar Devatha, Gopinath Taget & Jeremy Tammik (from DevTech India, Americas and Europe, respectively) for contributing to my knowledge in this area over the last few months (whether they knew they were doing so, or not :-).
This post shows how to make use of a handy interface inside AutoCAD to place custom settings in the Registry and how to then read them back. The code is very simple: you simply open up the current profile and then access/modify your hierarchy of setting beneath it. I've used a Registered Developer Symbol (RDS) to prefix the section of the Registry directly beneath the profile, to avoid conflicts with other applications.
There are other ways of saving more complex settings to the Registry: in a future post I'll go more in-depth with the System.Configuration namespace (especially how to implement your own System.Configuration.ConfigurationSettings class and save it to the Registry).
Here's the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
namespace ApplicationSettings
{
public class Commands
{
// We're using our Registered Developer Symbol (RDS)
// TTIF == Through the Interface
// for the section name.
// The entries beneath don't need this.
const string sectionName = "TTIFSettings";
const string intProperty = "TestInteger";
const string doubleProperty = "TestDouble";
const string stringProperty = "TestString";
[CommandMethod("ATR")]
static public void AddToRegistry()
{
IConfigurationSection con =
Application.UserConfigurationManager.OpenCurrentProfile();
using (con)
{
IConfigurationSection sec =
con.CreateSubsection(sectionName);
using (sec)
{
sec.WriteProperty(intProperty, 1);
sec.WriteProperty(doubleProperty, 2.0);
sec.WriteProperty(stringProperty, "Hello");
}
}
}
[CommandMethod("RFR")]
static public void RetrieveFromRegistry()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
IConfigurationSection prf =
Application.UserConfigurationManager.OpenCurrentProfile();
using (prf)
{
if (prf.ContainsSubsection(sectionName))
{
IConfigurationSection sec =
prf.OpenSubsection(sectionName);
using (sec)
{
double doubleValue =
(double)sec.ReadProperty(doubleProperty, 0.0);
string stringValue =
(string)sec.ReadProperty(stringProperty, "");
int intValue =
(int)sec.ReadProperty(intProperty, 0);
object defValue =
sec.ReadProperty("NotThere", 3.142);
ed.WriteMessage("\nInt value: " + intValue);
ed.WriteMessage("\nDouble value: " + doubleValue);
ed.WriteMessage("\nString value: " + stringValue);
ed.WriteMessage("\nNon-existent value: " + defValue);
}
}
}
}
}
}
Here's what we see when we run the RFR command (after having run the ATR beforehand, whether in the same session or a previous one):
Command: RFR
Int value: 1
Double value: 2
String value: Hello
Non-existent value: 3.142
And here are the contents of our new section of the Registry:
The observant among you will notice I've switched across to Vista (having just received a new machine). So far I've actually enjoyed using it, having disabled UAC within the first few minutes of getting it. :-)