从ProgID获取dll目录

时间:2012-04-18 16:54:40

标签: c# dll

我正在使用

加载一个dll
Type progID =  Type.GetTypeFromProgID(SimpleConfiguration.currentConfiguration.OPOSWrappedSO);

我还想将此dll的路径添加到PATH环境变量中。有没有办法从类型中获取目录?

由于

1 个答案:

答案 0 :(得分:2)

    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);
    }