Search code examples
c#windowscommand-linecommand-line-argumentsshortcut

How to avoid the redirect output sign">"being taken as command line arguments(in a shortcut maybe)


I'm not able to redirect standard output to the file by calling the shortcut on Windows.

it works directly "exe.exe 1 2 1 4 5 6 7 >> log.txt",the log file is generated successfully.

I assume that the ">>" sign is taken as an argument while using the shortcut.lnk, Thus it does not generate log.txt

how can I start the command properly or modify the program to achieve what I want?

here is my code:

static void Main(string[] args)
        {
            string acc = "";
            string pass = "";
            string EmailEnabled = "";
            string smtpServer = "";
            string sender = "";
            string smtpAuth = "";
            string receiver = "";
            if (args.Length == 7)
            {
                acc = args[0];
                pass = args[1];
                EmailEnabled = args[2];
                smtpServer = args[3];
                sender = args[4];
                smtpAuth = args[5];
                receiver = args[6];
            }
            else if (args[2] == "0")
            {
                acc = args[0];
                pass = args[1];
                EmailEnabled = args[2];
            }
            else
            {
                Console.WriteLine("command line arguments are wrong");
                return;
            }
}

Screenshots:

in this way,the">"is taken as an argument

in another way,the">" funtions normal as redirect sign


Solution

  • I believe the question is related to Windows more than C# so to redirect shortcut

    @Flydog57's comment

    cmd /c "file_shortcut.lnk" bla bla bla  >out.txt
    

    coming on C#: you can make 8th argument output file and redirect the standard output following this: console.setout
    probably something like this:

       using (var writer = args.Length>7? new StreamWriter(args[7]) : new StreamWriter(Console.OpenStandardOutput()))
            { 
                    Console.SetOut(writer); 
                   //all codes inside
            }