private GetCommand ( [ commandLine, [ additionalArgs ) : Command | ||
commandLine | [ | |
additionalArgs | [ | |
return | Command |
public void TestGetCommand() { 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 }); string additionalArgs; provider.GetCommand("installation directory" + Path.DirectorySeparatorChar + "entry.exe", out 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(); }
public void TestGetCommand() { 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 }); string additionalArgs; Assert.AreSame(commandNoArgs, provider.GetCommand("installation directory" + Path.DirectorySeparatorChar + "entry.exe", out additionalArgs)); Assert.AreEqual("", additionalArgs); Assert.AreSame(commandNoArgs, provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg1", out additionalArgs)); Assert.AreEqual("--arg1", additionalArgs); Assert.AreSame(commandArgs1, provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg1 \"long argument\" bla", out additionalArgs)); Assert.AreEqual("bla", additionalArgs); Assert.AreSame(commandArgs2, provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg2 \"long argument\" bla", out additionalArgs)); Assert.AreEqual("bla", additionalArgs); Assert.IsNull(provider.GetCommand("Something" + Path.DirectorySeparatorChar + "else.exe", out additionalArgs)); }
public void TestGetCommand() { 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}); string additionalArgs; provider.GetCommand("installation directory" + Path.DirectorySeparatorChar + "entry.exe", out 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(); }
private static Verb GetVerb([NotNull] RegistryKey typeKey, [NotNull] CommandMapper commandMapper, [NotNull] string verbName) { #region Sanity checks if (typeKey == null) { throw new ArgumentNullException("typeKey"); } if (string.IsNullOrEmpty(verbName)) { throw new ArgumentNullException("verbName"); } if (commandMapper == null) { throw new ArgumentNullException("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(); } string additionalArgs; var command = commandMapper.GetCommand(commandLine, out additionalArgs); if (command == null) { return(null); } string commandName = command.Name; if (commandName == Command.NameRun) { commandName = null; } var verb = new Verb { Name = verbName, Command = commandName, Arguments = additionalArgs }; if (!string.IsNullOrEmpty(description)) { verb.Descriptions.Add(description); } return(verb); } }
/// <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); }
public void TestGetCommand() { 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}); string additionalArgs; Assert.AreSame(commandNoArgs, provider.GetCommand("installation directory" + Path.DirectorySeparatorChar + "entry.exe", out additionalArgs)); Assert.AreEqual("", additionalArgs); Assert.AreSame(commandNoArgs, provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg1", out additionalArgs)); Assert.AreEqual("--arg1", additionalArgs); Assert.AreSame(commandArgs1, provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg1 \"long argument\" bla", out additionalArgs)); Assert.AreEqual("bla", additionalArgs); Assert.AreSame(commandArgs2, provider.GetCommand("\"installation directory" + Path.DirectorySeparatorChar + "entry.exe\" --arg2 \"long argument\" bla", out additionalArgs)); Assert.AreEqual("bla", additionalArgs); Assert.IsNull(provider.GetCommand("Something" + Path.DirectorySeparatorChar + "else.exe", out additionalArgs)); }