/// <summary>
/// Places the requested catalog to first in the list of MvParts.
/// </summary>
/// <param name="catalogName">The catalog to move to the first position</param>
/// <returns>ErrorStatus.Ok if the operation succeeded</returns>
/// <returns>ErrorStatus.KeyNotFound if the requested catalog was not found</returns>
/// <returns>ErrorStatus.RegistryAccessError if there was a problem accessing the catalogs</returns>
public static ErrorStatus SetFirstCatalog(string catalogName)
{
/// TODO: Automate the release year of the registry location. Currently this code only works
/// with Autocad MEP 2014. Use the system variable ACADVER
const string subkey1 = "Software\\Autodesk\\AutoCAD\\R19.1\\ACAD-D006:409\\Profiles\\";
const string subkey2 = "\\Preferences\\AecbCatalog70\\MvPartContent";
string profile = Application.GetSystemVariable("CPROFILE").ToString();
string subkey = subkey1 + profile + subkey2;
string catalogPath = "";
string pathZero = "";
bool found = false;
int foundLocation = 0;
try
{
// Since the number of catalogs is unknown then we must start at the beginning and work our way through each
// catalog. If the requested catalog is in the first position then we can just break from the search and
// return ok. Otherwise save the first catalog path and once the requested catalog is found the first postion
// and the requested catalog position can be switch. The only position that really matters is the position of
// the first catalog. Other catalog positions are irrelevant.
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree);
foreach (string valueName in registryKey.GetValueNames())
{
if (foundLocation == 0)
{
if (catalogName.ToUpper() == Path.GetFileNameWithoutExtension(registryKey.GetValue(valueName).ToString().ToUpper()))
{
return ErrorStatus.OK;
}
pathZero = registryKey.GetValue(valueName).ToString();
}
if (catalogName.ToUpper() == Path.GetFileNameWithoutExtension(registryKey.GetValue(valueName).ToString().ToUpper()))
{
catalogPath = registryKey.GetValue(valueName).ToString();
found = true;
break;
}
foundLocation++;
}
// If we are at this point then we have either searched the entire list of catalogs
// and did not find the requested catalog or we did find the requested catalog.
if (found && catalogPath != "" && pathZero != "")
{
registryKey.SetValue("PATH" + foundLocation.ToString(), pathZero);
registryKey.SetValue("PATH0", catalogPath);
return ErrorStatus.OK;
}
return ErrorStatus.KeyNotFound;
}
// Any exceptions that we catch will mean that there was a Registry Access error
// We can just return the correct ErrorStatus and have the calling method handle it.
// If there is a exception then the catalog order will just have to be set manually.
catch (System.Exception exception)
{
return ErrorStatus.RegistryAccessError;
}
}