Example #1
0
 /// <summary>
 /// Determines whether the GAC contains the specified code base URI.
 /// </summary>
 /// <param name="codeBaseUri">The code base URI.</param>
 public static bool Contains(Uri codeBaseUri)
 {
     if (codeBaseUri == null)
     {
         Debug.Fail("codeBaseUri == null"); return(false);
     }
     lock (GlobalLock.LockingObject)
     {
         if (!GlobalAssemblyCache.s_fusionLoaded)
         {
             GlobalAssemblyCache.s_fusionLoaded = true;
             System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
             //^ assume systemAssembly != null;
             string /*?*/ systemAssemblyLocation = systemAssembly.Location;
             //^ assume systemAssemblyLocation != null;
             string dir = Path.GetDirectoryName(systemAssemblyLocation);
             //^ assume dir != null;
             GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
         }
         IAssemblyEnum assemblyEnum;
         int           rc = GlobalAssemblyCache.CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
         if (rc < 0 || assemblyEnum == null)
         {
             return(false);
         }
         IApplicationContext applicationContext;
         IAssemblyName       currentName;
         while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
         {
             //^ assume currentName != null;
             AssemblyName assemblyName = new AssemblyName(currentName);
             string /*?*/ scheme       = codeBaseUri.Scheme;
             if (scheme != null && assemblyName.CodeBase.StartsWith(scheme))
             {
                 try
                 {
                     Uri foundUri = new Uri(assemblyName.CodeBase);
                     if (codeBaseUri.Equals(foundUri))
                     {
                         return(true);
                     }
                 }
                 catch (System.ArgumentNullException)
                 {
                 }
                 catch (System.UriFormatException)
                 {
                 }
             }
         }
         return(false);
     }
 }
Example #2
0
        /// <summary>
        /// Determines whether the GAC contains the specified code base URI.
        /// </summary>
        /// <param name="codeBaseUri">The code base URI.</param>
        public static bool Contains(Uri codeBaseUri)
        {
            if (codeBaseUri == null)
            {
                Debug.Fail("codeBaseUri == null"); return(false);
            }
            lock (GlobalLock.LockingObject) {
#if COMPACTFX
                var gacKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"\Software\Microsoft\.NETCompactFramework\Installer\Assemblies\Global");
                if (gacKey == null)
                {
                    return(false);
                }
                var codeBase = codeBaseUri.AbsoluteUri;
                foreach (var gacName in gacKey.GetValueNames())
                {
                    var values = gacKey.GetValue(gacName) as string[];
                    if (values == null || values.Length == 0)
                    {
                        continue;
                    }
                    if (string.Equals(values[0], codeBase, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                    if (values.Length > 1 && string.Equals(values[1], codeBase, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
                return(false);
#else
                if (!GlobalAssemblyCache.FusionLoaded)
                {
                    GlobalAssemblyCache.FusionLoaded = true;
                    System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
                    //^ assume systemAssembly != null;
                    string /*?*/ systemAssemblyLocation = systemAssembly.Location;
                    //^ assume systemAssemblyLocation != null;
                    string dir = Path.GetDirectoryName(systemAssemblyLocation);
                    //^ assume dir != null;
                    GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
                }
                IAssemblyEnum assemblyEnum;
                int           rc = GlobalAssemblyCache.CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
                if (rc < 0 || assemblyEnum == null)
                {
                    return(false);
                }
                IApplicationContext applicationContext;
                IAssemblyName       currentName;
                while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
                {
                    //^ assume currentName != null;
                    AssemblyName assemblyName = new AssemblyName(currentName);
                    string /*?*/ scheme       = codeBaseUri.Scheme;
                    if (scheme != null && assemblyName.CodeBase.StartsWith(scheme))
                    {
                        try {
                            Uri foundUri = new Uri(assemblyName.CodeBase);
                            if (codeBaseUri.Equals(foundUri))
                            {
                                return(true);
                            }
                        } catch (System.ArgumentNullException) {
                        } catch (System.UriFormatException) {
                        }
                    }
                }
                return(false);
#endif
            }
        }