Passing newlines in command line argument?

I downloaded a small console application that just reads and prints the first command line argument passed to the program on the console.

I want to pass a new line to the argument I tried:

prog.exe \n  --> outputs \n
prog.exe "sfs \n sfff" --> outputs sfs \n sfff
prog.exe "ff \\n ff" --> outputs ff \\n ff
prog .exe "ff \\\\n ff" --> outputs ff \\\\n ff

Are there any other screens that I can use? Or is there some kind of function that I have to call args [0] to process escaped characters before output to the console?

To clarify, I'm trying to pass a string to my program that has newline characters in it. The output to the console was used as a test. I could just as easily insert a debug interrupt line and check the contents of the variable.

+5
source share
4 answers

, , , , , . , , escape- .. , , - , , "\n" .

Edit:

( ), , CSharp, escape- :

    public static string ParseString(string input)
    {
        var provider = new Microsoft.CSharp.CSharpCodeProvider();
        var parameters = new System.CodeDom.Compiler.CompilerParameters()
        {
            GenerateExecutable = false,
            GenerateInMemory = true,
        };

        var code = @"
        public class TmpClass
        {
            public static string GetValue()
            {
                return """ + input + @""";
            }
        }";

        var compileResult = provider.CompileAssemblyFromSource(parameters, code);

        if (compileResult.Errors.HasErrors)
        {
            throw new ArgumentException(compileResult.Errors.Cast<System.CodeDom.Compiler.CompilerError>().First(e => !e.IsWarning).ErrorText);
        }

        var asmb = compileResult.CompiledAssembly;
        var method = asmb.GetType("TmpClass").GetMethod("GetValue");

        return method.Invoke(null, null) as string;
    }
+2

, , . PowerShell:

PS> ./echoargs "a`nb" b c
arg 1: a
b
arg 2: b
arg 3: c

, cmd, ( - Process.Start , ).

+2

From the Windows command line, this is not possible. For security, C # parses user input from the command line as completely literal. There are no escape characters that will automatically create a new line unless you write code to go through the line and replace with the \nactual newline character.

If you type \nin the command line, C # will parse this as \\n.

+1
source

If you pass prog .exe "ff \\\\n ff"

you can replace them inside the code var s = arg[0[.Replace("\" ,@"\\");as follows

0
source

All Articles