How to use CSI.exe script argument

前端 未结 2 1144
遥遥无期
遥遥无期 2021-02-19 06:41

When you run csi.exe /? (with Visual Studio 2015 update 2 installed), you will get the following syntax

Microsoft (R) Visual C# Interactive Compiler version 1.2.         


        
相关标签:
2条回答
  • 2021-02-19 07:08

    There is a global variable in scripts called Args which has these "script argument" values. The closest thing I can find to documentation is mention of it in pull requests for the roslyn repo. In a csx file (test.csx):

    using System;
    Console.WriteLine("Hello {0}!", Args[0]);
    

    using the command line:

    csi.exe test.csx arg1
    

    will give the output:

    Hello arg1!

    An alternative approach using Environment.GetCommandLineArgs() could be made to work, but the problem is that this picks up all the arguments passed to csi process. Then you have to separate the "script arguments" from the options for csi itself. This work can be avoided by using the builtin Args variable which is going to be more maintainable as well.

    0 讨论(0)
  • 2021-02-19 07:16

    You can use Environment.GetCommandLineArgs() for that.

    Example for your example:

    using System;
    Console.WriteLine("Hello {0}!", Environment.GetCommandLineArgs()[2]);
    

    Notice that I'm reading the third item, because Environment.GetCommandLineArgs() gives the entire command line (if you run the script using csi test.csx David, the first one will be csi and the second one test.csx)

    0 讨论(0)
提交回复
热议问题