Esempio n. 1
0
        /// <summary>
        ///     Provides filtering and sorting options, and returns a string
        ///     <see cref="List{T}"/> containing the command-line arguments for the current
        ///     process.
        /// </summary>
        /// <param name="sort">
        ///     <see langword="true"/> to sort the arguments ascended with the rules of
        ///     <see cref="AlphaNumericComparer(bool)"/> before returning the arguments;
        ///     otherwise, <see langword="false"/>.
        /// </param>
        /// <param name="skip">
        ///     The number of arguments to skip before returning the remaining arguments.
        /// </param>
        /// <param name="quotes">
        ///     <see langword="true"/> to store the arguments in quotation marks which
        ///     containing spaces; otherwise, <see langword="false"/>.
        /// </param>
        public static IList <string> CommandLineArgs(bool sort = true, int skip = 1, bool quotes = true)
        {
            if (_cmdLineArgs?.Count == Environment.GetCommandLineArgs().Length - skip && quotes == _cmdLineArgsQuotes)
            {
                return(_cmdLineArgs);
            }
            _cmdLineArgs       = new List <string>();
            _cmdLineArgsQuotes = quotes;
            if (Environment.GetCommandLineArgs().Length <= skip)
            {
                return(_cmdLineArgs);
            }
            string arg         = null;
            var    defaultArgs = Environment.GetCommandLineArgs().ToList();

            if (defaultArgs.Any(x => (arg = x).EqualsEx($"/{Log.DebugKey}")))
            {
                var index = defaultArgs.IndexOf(arg);
                defaultArgs.RemoveAt(index);
                if (defaultArgs.Count > index)
                {
                    defaultArgs.RemoveAt(index);
                }
            }
            if (defaultArgs.Count > skip)
            {
                defaultArgs = defaultArgs.Skip(skip).ToList();
            }
            if (sort)
            {
                defaultArgs = defaultArgs.OrderBy(x => x, CacheInvestor.GetDefault <AlphaNumericComparer <string> >()).ToList();
            }
            return(_cmdLineArgs = quotes ? defaultArgs.Select(x => x.Any(char.IsWhiteSpace) ? $"\"{x}\"" : x).ToList() : defaultArgs);
        }
Esempio n. 2
0
 /// <summary>
 ///     Returns the display names of all installed Microsoft Visual C++
 ///     redistributable packages.
 /// </summary>
 /// <param name="refresh">
 ///     <see langword="true"/> to refresh all names; otherwise,
 ///     <see langword="false"/> to get the cached names from previous call.
 ///     <para>
 ///         Please note that this parameter is always <see langword="true"/> if
 ///         this function has never been called before.
 ///     </para>
 /// </param>
 public static string[] GetDisplayNames(bool refresh = false)
 {
     try
     {
         if (!refresh && _displayNames != null)
         {
             return(_displayNames);
         }
         var comparer = CacheInvestor.GetDefault <AlphaNumericComparer <string> >();
         var entries  = new[]
         {
             "DisplayName",
             "ProductName"
         };
         var names = Reg.GetSubKeyTree(Registry.LocalMachine, "SOFTWARE\\Classes\\Installer", 3000)
                     .SelectMany(x => entries, (x, y) => Reg.ReadString(Registry.LocalMachine, x, y))
                     .Where(x => x.StartsWithEx("Microsoft Visual C++"))
                     .OrderBy(x => x, comparer);
         _displayNames = names.ToArray();
         return(_displayNames);
     }
     catch (Exception ex) when(ex.IsCaught())
     {
         Log.Write(ex);
         return(null);
     }
 }
Esempio n. 3
0
        internal static IDictionary <string, IDictionary <string, IList <string> > > SortHelper(IDictionary <string, IDictionary <string, IList <string> > > source, IEnumerable <string> topSections)
        {
            var comparer = CacheInvestor.GetDefault <AlphaNumericComparer <string> >();

            if (topSections != null)
            {
                return(source.OrderBy(p => !string.IsNullOrEmpty(p.Key)) // non-section is string.Empty and should be always on top
                       .ThenBy(p => !IsImportantSection(p.Key))          // important sections should also always be on top
                       .ThenBy(p => !topSections.Contains(p.Key))        // sequence of sections to keep on top (optional)
                       .ThenBy(p => p.Key, comparer)                     // sort sections alphabetical and numerical
                       .ToDictionary(p => p.Key, p => p.Value));         // finally, create the sorted dictionary
            }
            return(source.OrderBy(p => !string.IsNullOrEmpty(p.Key))
                   .ThenBy(p => !IsImportantSection(p.Key))
                   .ThenBy(p => p.Key, comparer)
                   .ToDictionary(p => p.Key, p => p.Value));
        }