Пример #1
0
        /// <summary>
        /// Given a directory with fonts, installs all fonts in the directory
        /// </summary>
        /// <param name="path"></param>
        public void InstallFont(string path)
        {
            string fontDestination = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

            foreach (var file in Directory.EnumerateFiles(path, "*.otf"))
            {
                var faces = Fonts.GetTypefaces("file:///" + file).ToList();
                var face  = faces.First();

                List <string> fontNameParts = new List <string>();

                // Build the font name...
                bool   trueType   = Path.GetExtension(file)?.Equals(".otf", StringComparison.CurrentCultureIgnoreCase) == true;
                string familyName = face.FontFamily.ToString().Split('#')[face.FontFamily.ToString().Split('#').Count() - 1];

                fontNameParts.Add(familyName);
                fontNameParts.Add(face.FaceNames.First().Value);

                if (trueType)
                {
                    fontNameParts.Add("(TrueType)");
                }

                string fontName = string.Join(" ", fontNameParts.Where((i) => !string.IsNullOrWhiteSpace(i)));

                var existingFontRegistryKey = UtilsRegistry.GetRegistryKeyValue32(
                    RegistryHive.LocalMachine,
                    @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                    fontName,
                    null);

                // Si la fuenta ya existe, no hacemos nada.
                if (existingFontRegistryKey != null)
                {
                    continue;
                }

                RegisterFont(file, fontName, fontDestination);
            }
        }
Пример #2
0
        private static List <Handle> GetProcessesThatBlockPathHandle(string path, ILoggerInterface logger, bool logDetails = false)
        {
            if (!File.Exists(path) && !Directory.Exists(path))
            {
                return(new List <Handle>());
            }

            string key  = "SOFTWARE\\Sysinternals\\Handle";
            string name = "EulaAccepted";

            // This Utility has an EULA GUI on first run... try to avoid that
            // by manually setting the registry
            int?eulaaccepted64 = (int?)UtilsRegistry.GetRegistryKeyValue64(RegistryHive.CurrentUser, key, name, null);
            int?eulaaccepted32 = (int?)UtilsRegistry.GetRegistryKeyValue32(RegistryHive.CurrentUser, key, name, null);

            bool eulaaccepted = (eulaaccepted32 == 1 && eulaaccepted64 == 1);

            if (!eulaaccepted)
            {
                UtilsRegistry.SetRegistryValue(RegistryHive.CurrentUser, key, name, 1, RegistryValueKind.DWord);
            }

            // Normalize the path, to ensure that long path is not used, otherwise handle.exe won't work as expected
            string fileName = UtilsSystem.RemoveLongPathSupport(path);

            List <Handle> result     = new List <Handle>();
            string        outputTool = string.Empty;

            // Gather the handle.exe from the embeded resource and into a temp file
            var handleexe = UtilsSystem.GetTempPath("handle") + Guid.NewGuid().ToString().Replace("-", "_") + ".exe";

            UtilsSystem.EmbededResourceToFile(Assembly.GetExecutingAssembly(), "_Resources.Handle.exe", handleexe);

            try
            {
                using (Process tool = new Process())
                {
                    tool.StartInfo.FileName               = handleexe;
                    tool.StartInfo.Arguments              = fileName;
                    tool.StartInfo.UseShellExecute        = false;
                    tool.StartInfo.Verb                   = "runas";
                    tool.StartInfo.RedirectStandardOutput = true;
                    tool.Start();
                    outputTool = tool.StandardOutput.ReadToEnd();
                    tool.WaitForExit(1000);

                    if (!tool.HasExited)
                    {
                        tool.Kill();
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogException(e, EventLogEntryType.Warning);
            }
            finally
            {
                UtilsSystem.DeleteFile(handleexe, logger, 5);
            }

            string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";

            foreach (Match match in Regex.Matches(outputTool, matchPattern))
            {
                if (int.TryParse(match.Value, out var pid))
                {
                    if (result.All(i => i.pid != pid))
                    {
                        result.Add(new Handle()
                        {
                            pid = pid
                        });
                    }
                }
            }

            if (result.Any() && logDetails)
            {
                logger?.LogInfo(true, outputTool);
            }

            return(result);
        }