probably at the beach
probably at the beach

Reputation: 15227

Get dll directory from ProgID

I'm loading a dll using

Type progID =  Type.GetTypeFromProgID(SimpleConfiguration.currentConfiguration.OPOSWrappedSO);

I also want to add the path of this dll to the PATH environment variable. Is there a way to get the Directory from the type?

Thanks

Upvotes: 2

Views: 2729

Answers (1)

probably at the beach
probably at the beach

Reputation: 15227

    public static string GetDLLPathFromClassID(string classID)
    {
        var regPath = @"\CLSID\" + classID + @"\InProcServer32\";
        return  GetDefaultRegistryValue(Registry.ClassesRoot, regPath);
    }

    public static string GetClassIDFromProgID(string progID)
    {
            var regPath =   progID + @"\CLSID\";
            return  GetDefaultRegistryValue(Registry.ClassesRoot, regPath);
    }

    private static string GetDefaultRegistryValue(RegistryKey rootKey, string regPath)
    {
        try
        {
            var regPermission = new RegistryPermission(RegistryPermissionAccess.Read,
                                                       @"HKEY_CLASSES_ROOT\" + regPath);
            regPermission.Demand();
            using (var regKey = rootKey.OpenSubKey(regPath))
            {
                if (regKey != null)
                {
                    string defaultValue = (string) regKey.GetValue("");
                    {
                        return defaultValue;
                    }
                }
            }
        }catch(Exception e)
        {
           //log error
        }
        return "";
    }

    public static string GetDLLDirectoryFromProgID(string progID)
    {
        var classID = GetClassIDFromProgID(progID);
        var fileName = GetDLLPathFromClassID(classID);
        if(string.IsNullOrEmpty(fileName))
        {
            return "";
        }
        return Path.GetDirectoryName(fileName);
    }

Upvotes: 4

Related Questions