问题
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.0.51106
Copyright (C) Microsoft Corporation. All rights reserved.
Usage: csi [option] ... [script-file.csx] [script-argument] ...
I am just wondering how I can pass this [script-argument] into my csx script file. Let's say, my csx script (c:\temp\a.csx) has only 2 line as follows
using System;
Console.WriteLine("Hello {0} !", <argument_from_commandLine>);
What I expect is after I run the following command line
csi.exe c:\temp\a.csx David
I will get
Hello David !
But I just do not know what I should do in my script file so I can pass the csi.exe [script_argument] to my script file (to replace ).
Thanks in advance for your time and help.
回答1:
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.
回答2:
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
)
来源:https://stackoverflow.com/questions/38060860/how-to-use-csi-exe-script-argument