I've coded this as an exercise in answer to a question elsewhere.
The p/Envoke for GetEnv and SetEnv seemed the easiest way to handle the Variables stored in the Fixed Profile ->General section of the registry.
.. Other Variables are stored in Profiles-> <<UserProfile>> -> General
any tips for improvement appreciated.
// (C) CodeHimBelonga kdub @TheSwamp 20091017
//
using System;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(KDUB_testing.MyTestCommands))]
namespace KDUB_testing
{
public class MyTestCommands
{
// p/Envoke from Acad.exe
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe",
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.Cdecl)]
private static extern int acedGetEnv(string envName, StringBuilder ReturnValue);
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe",
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.Cdecl)]
private static extern int acedSetEnv(string envName, StringBuilder NewValue);
//--------------
[CommandMethod("DoIt", CommandFlags.Modal)]
public void MyGetEnv()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
string mainCuiFile = AcadApp.GetSystemVariable("MENUNAME") as string;
ed.WriteMessage("\nMenuName : {0}", mainCuiFile);
const int maxResultLength = 1024;
StringBuilder sbRes = new StringBuilder(maxResultLength);
acedGetEnv("ACAD", sbRes);
ed.WriteMessage("\nValue of ACAD environment variable: {0}", sbRes.ToString());
acedGetEnv("AcetMoveBak", sbRes);
ed.WriteMessage("\n\nMoveBak Variable: {0}", sbRes.ToString());
//--------------
// assume the folder exists for this exercise
StringBuilder sbResNew = new StringBuilder("c:\\DWG-BAK\\Auto");
acedSetEnv("AcetMoveBak", sbResNew);
acedGetEnv("AcetMoveBak", sbRes);
ed.WriteMessage("\n\nMoveBak Variable is Now : {0}", sbRes.ToString());
}
}
}