コード例 #1
0
        private CommandSpec CreateCommandSpecWrappedWithCmd(
            string command,
            IEnumerable <string> args)
        {
            var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";

            // Handle the case where ComSpec is already the command
            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args    = args.Skip(1);
            }

            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (!ArgumentEscaper.IsSurroundedWithQuotes(command) && // Don't quote already quoted strings
                ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return(new CommandSpec(comSpec, escapedArgString));
        }
コード例 #2
0
        public void IsSurroundedWithQuotesShouldReturnFalseIfStringIsNotSurrondedByQuotes()
        {
            string stringNotSurroundWithQuotes = "some string";

            Assert.IsFalse(ArgumentEscaper.IsSurroundedWithQuotes(stringNotSurroundWithQuotes));
        }
コード例 #3
0
        public void IsSurroundedWithQuotesShouldReturnTrueIfStringIsSurrondedByQuotes()
        {
            string stringSurroundWithQuotes = "\"some string\"";

            Assert.IsTrue(ArgumentEscaper.IsSurroundedWithQuotes(stringSurroundWithQuotes));
        }