Storing custom AutoCAD application settings in the Registry using .NET

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:

Custom application settings in 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. 🙂

14 responses to “Storing custom AutoCAD application settings in the Registry using .NET”

  1. Fernando Malard Avatar
    Fernando Malard

    Hi Kean,

    Yes, disable UAC...I have done this on my first Vista day. Guess UAC is more suitable for commom users.

    Regarding to this post, I remember some articles about Microsoft saying that registry will be removed soon from Windows on the next releases.
    In this case, there is a better solution to store user settings (by profile) which is through XML configuration files.

    What are Autodesk plans regarding AutoCAD user profile settings, windows Registry and XML configuration files?

    Could give us some trends and overview bout this so we can avoid investing time into features that will be soon deprecated?

    Regards,
    Fernando Malard.

  2. Kean Walmsley Avatar
    Kean Walmsley

    We'll see - I wouldn't worry about the Registry disappearing just yet, and frankly I'd expect the API used in this example to simply write to wherever we store profile information in that Brave New World. 🙂

    As mentioned above, I'm planning on looking more into System.Configuation in a future post - hopefully this go some way to address your question about config files vs. the Registry.

    Kean

  3. Danny Polkinhorn Avatar
    Danny Polkinhorn

    Hi Kean,

    Excellent post. Looking forward to the next one too!

    Is it possible to bind a windows control to one of these registry settings? For example, a text box to your "TestString" property. I'm already planning an additional tab in the Preferences dialog, and this would eliminate quite a bit of additional validation code. Maybe I need to create a custom class that handles the registry settings and bind the control to a property of the class?

  4. Jose Madrigal Avatar

    Kean,

    I'm getting exception reading:
    Value exists but is not a string.

    Only get this when attempting to read values. Any clues?

  5. Kean Walmsley Avatar

    Danny -

    I believe this is supported by System.Configuration, but I'll need to do some research. And I have a few other posts that may come in between, I'm afraid.

    Jose -

    Did you change the code, at all? It sounds as though you have a "type mismatch" between what was written and what you're trying to read back in.

    Kean

  6. Jose Madrigal Avatar
    Jose Madrigal

    Kean,

    I forgot to thank you for the post. I was at home this weekend working on this subject(without an internet connection--Arghh!!) and as if you were reading my mind, produced this excellent post. As I mentioned before, I modified your code to read settingst like "IsPureAcadProfile" which is a value directly under the current profile for instance, and I keep getting exception: Value exists but is not a string.

    [CommandMethod("PrintConfig")]
    static public void PrintConfig()
    {
    try
    {
    var cs = AcadApp.UserConfigurationManager.OpenCurrentProfile();
    using (cs)
    {
    if (cs.Contains("IsPureAcadProfile"))
    {
    AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nValue Exists");
    }
    }
    }
    catch (System.Exception ex)
    {

    AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
    }

    Command: printconfig
    System.InvalidOperationException: Value exist but it is not a string.
    at ConfigurationSectionOnRegistry.Contains(String name)
    at BGA.CAD.Config.Client.Commands.PrintConfig() in
    C:\Users\Administrator\Documents\Visual Studio
    2008\Projects\BGA.CAD.Config\BGA.CAD.Config.Client\Commands.cs:line 152

    I get this with any setting I try to read, regardless of the section. Your code however, works perfectly.

    Jose

  7. Kean Walmsley Avatar

    It appears that the values stored in the Registry via the API all get converted to strings (type REG_SZ). This API doesn't appear to work for reading arbitrary values of non-string type (such as REG_DWORD).

    So it's good for storing/retrieving your own values in a profile, but not for retrieving profile settings - you should stick to standard Registry access techniques for those.

    Kean

  8. Jose Madrigal Avatar
    Jose Madrigal

    I suppose I learned that one the hard way eh? Thanks again for your post. Really could not have had better timing.

    Best Regards,

    Jose

  9. This is pretty good. But do you know how to add file search paths to the support file search path in the options dialog box?

  10. That's another question: you would need to modify a system variable inside AutoCAD or change a Registry setting (not recommended, but very possible). For specifics I'd suggest posting/searching the AutoCAD .NET Discussion Group or the ADN website, if you're a member.

    Kean

  11. Kean,

    Is there a way to refresh an AutoCAD session once it's been modified via this method? I'm modifying default AutoCAD settings.

  12. Not that I'm aware of.

    Kean

  13. Jürgen A. Becker Avatar
    Jürgen A. Becker

    Hi Kean,
    not seen for a long time.
    I hope everythink is ok and you and your family is healty.

    Now I want to read a profile file (*.arg).
    Can I do that with that statement?
    Autodesk.AutoCAD.ApplicationServices.Application.UserConfigurationManager.SetCurrentProfile("FileName");

    thanks for helping.

    Regards Jürgen

    1. Hi Jürgen,

      It's nice to hear from you. Unfortunately I'm not working with AutoCAD, these days: please post your question to the AutoCAD .NET forum and someone more up-to-date will be able to answer you.

      Best regards,

      Kean

Leave a Reply to DWolfe Cancel reply

Your email address will not be published. Required fields are marked *