コード例 #1
0
        /// <exclude />
        protected override LicenseProviderResult Load()
        {
            try
            {
                if (!File.Exists(FullPath))
                {
                    return(LicenseProviderResult.FromErrorMessage(Messages.FileNotFound));
                }

                string license = File.ReadAllText(FullPath);
                if (string.IsNullOrEmpty(license))
                {
                    return(LicenseProviderResult.FromErrorMessage(Messages.EmptyFile));
                }
                else
                {
                    return(LicenseProviderResult.FromLicense(license));
                }
            }
            catch (IOException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
            catch (UnauthorizedAccessException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
            catch (SecurityException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
        }
コード例 #2
0
        /// <exclude />
        protected override LicenseProviderResult Load()
        {
            bool isGranted = false;

            try
            {
                string             subKey     = SubkeyNameToOpen;
                RegistryPermission permission = new RegistryPermission(PermissionState.Unrestricted);
                isGranted = SecurityManager.IsGranted(permission);
                if (isGranted)
                {
                    permission.Assert();
                }
                using (RegistryKey registryKey = RegistryKey.OpenSubKey(subKey))
                {
                    if (registryKey == null)
                    {
                        return(LicenseProviderResult.FromErrorMessage(Messages.OpenRegistryKeyFailed));
                    }

                    string license = (string)registryKey.GetValue(ValueName, null);
                    if (string.IsNullOrEmpty(license))
                    {
                        return(LicenseProviderResult.FromErrorMessage(Messages.EmptyRegistryValue));
                    }
                    else
                    {
                        return(LicenseProviderResult.FromLicense(license));
                    }
                }
            }
            catch (SecurityException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadRegistryFailed(exception.Message)));
            }
            finally
            {
                if (isGranted)
                {
                    CodeAccessPermission.RevertAssert();
                }
            }
        }
コード例 #3
0
        private LicenseProviderResult GetLicenseFromCallingAssembly()
        {
            List <StackFrame> stackFrames = IsTraceEnabled ? new List <StackFrame>() : null;
            StackTrace        stackTrace  = new StackTrace();

            for (int i = 0; i < stackTrace.FrameCount; i++)
            {
                StackFrame stackFrame    = stackTrace.GetFrame(i);
                Type       reflectedType = stackFrame.GetMethod().ReflectedType;
                if (reflectedType == null)
                {
                    continue;
                }
                Assembly callingAssembly = reflectedType.Assembly;
                if (callingAssembly == Assembly)
                {
                    continue;
                }

                if (IsTraceEnabled)
                {
                    stackFrames.Add(stackFrame);
                }
                string license;
                if (CachedLicenses.ContainsKey(callingAssembly))
                {
                    license = CachedLicenses[callingAssembly];
                }
                else
                {
                    license = GetLicenseFromAssembly(callingAssembly);
                    CachedLicenses.Add(callingAssembly, license);
                }

                if (license != null)
                {
                    return(LicenseProviderResult.FromLicense(license, callingAssembly));
                }
            }

            return(LicenseProviderResult.FromErrorMessage(Messages.FormatEmbeddedResourceNotFound(AssemblyLicenseFileName), stackFrames));
        }
コード例 #4
0
        private LicenseProviderResult LoadFromEntryAssembly()
        {
            Assembly entryAssembly = Assembly.GetEntryAssembly();

            if (entryAssembly == null)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatEmbeddedResourceNotFound(AssemblyLicenseFileName)));
            }

            string license = GetLicenseFromAssembly(entryAssembly);

            if (string.IsNullOrEmpty(license))
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatEmbeddedResourceNotFound(AssemblyLicenseFileName)));
            }
            else
            {
                return(LicenseProviderResult.FromLicense(license, entryAssembly));
            }
        }
コード例 #5
0
        protected override LicenseProviderResult Load()
        {
            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetStore(Scope, ApplicationEvidenceType))
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(Path, FileMode.Open, FileAccess.Read, store))
                    {
                        if (stream == null)
                        {
                            return(LicenseProviderResult.FromErrorMessage(Messages.FileNotFound));
                        }

                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string xaml = reader.ReadToEnd();
                            if (string.IsNullOrEmpty(xaml))
                            {
                                return(LicenseProviderResult.FromErrorMessage(Messages.EmptyFile));
                            }
                            else
                            {
                                return(LicenseProviderResult.FromLicense(reader.ReadToEnd()));
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FileNotFound));
            }
            catch (SecurityException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
            catch (IsolatedStorageException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
        }