Example #1
0
    public void GetCommand()
    {
        var commandNoArgs = new Command {
            Name = "no-args", Path = "entry.exe"
        };
        var commandArgs1 = new Command {
            Name = "args1", Path = "entry.exe", Arguments = { "--arg1", "long argument" }
        };
        var commandArgs2 = new Command {
            Name = "args2", Path = "entry.exe", Arguments = { "--arg2", "long argument" }
        };
        var provider = new CommandMapper("installation directory", new[] { commandNoArgs, commandArgs1, commandArgs2 });


        provider.GetCommand("installation directory" + Path.DirectorySeparatorChar + "entry.exe", out string additionalArgs)
        .Should().BeSameAs(commandNoArgs);
        additionalArgs.Should().Be("");

        provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg1", out additionalArgs)
        .Should().BeSameAs(commandNoArgs);
        additionalArgs.Should().Be("--arg1");

        provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg1 \"long argument\" bla", out additionalArgs)
        .Should().BeSameAs(commandArgs1);
        additionalArgs.Should().Be("bla");

        provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg2 \"long argument\" bla", out additionalArgs)
        .Should().BeSameAs(commandArgs2);
        additionalArgs.Should().Be("bla");

        provider.GetCommand("Something" + Path.DirectorySeparatorChar + "else.exe", out additionalArgs)
        .Should().BeNull();
    }
Example #2
0
    /// <summary>
    /// Retrieves data about a verb (an executable command) from the registry.
    /// </summary>
    /// <param name="typeKey">The registry key containing information about the file type / protocol the verb belongs to.</param>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="verbName">The internal name of the verb.</param>
    /// <returns>The detected <see cref="Verb"/> or an empty <see cref="Verb"/> if no match was found.</returns>
    private static Verb?GetVerb(RegistryKey typeKey, CommandMapper commandMapper, string verbName)
    {
        #region Sanity checks
        if (typeKey == null)
        {
            throw new ArgumentNullException(nameof(typeKey));
        }
        if (string.IsNullOrEmpty(verbName))
        {
            throw new ArgumentNullException(nameof(verbName));
        }
        if (commandMapper == null)
        {
            throw new ArgumentNullException(nameof(commandMapper));
        }
        #endregion

        using var verbKey = typeKey.OpenSubKey(@"shell\" + verbName);
        if (verbKey == null)
        {
            return(null);
        }

        string?description = verbKey.GetValue("")?.ToString();
        string?commandLine;
        using (var commandKey = verbKey.OpenSubKey("command"))
        {
            if (commandKey == null)
            {
                return(null);
            }
            commandLine = commandKey.GetValue("")?.ToString();
        }

        if (string.IsNullOrEmpty(commandLine))
        {
            return(null);
        }
        var command = commandMapper.GetCommand(commandLine, out string?additionalArgs);
        if (command == null)
        {
            return(null);
        }

        var verb = new Verb
        {
            Name             = verbName,
            Command          = (command.Name == Command.NameRun) ? null : command.Name,
            ArgumentsLiteral = additionalArgs
        };
        if (!string.IsNullOrEmpty(description))
        {
            verb.Descriptions.Add(description);
        }
        return(verb);
    }