Пример #1
0
        /// <summary>
        /// Installs font on the user's system and adds it to the registry so it's available on the next session
        /// Your font must be included in your project with its build path set to 'Content' and its Copy property
        /// set to 'Copy Always'
        /// </summary>
        /// <param name="fontFilePath">Your font to be passed as a resource (i.e. "myfont.tff")</param>
        /// <param name="fontName"></param>
        /// <param name="fontDestination"></param>
        private static void RegisterFont(string fontFilePath, string fontName, string fontDestination)
        {
            string fontFileName = Path.GetFileName(fontFilePath);

            // Creates the full path where your font will be installed
            string fontFileDestinationSystemPath = Path.Combine(fontDestination, fontFileName);

            if (!File.Exists(fontFileDestinationSystemPath))
            {
                // Copies font to destination
                File.Copy(fontFilePath, fontFileDestinationSystemPath);
            }

            UtilsRegistry.SetRegistryValue(
                RegistryHive.LocalMachine,
                @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                fontName,
                fontFileName,
                RegistryValueKind.String);
        }
Пример #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);
        }