Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何访问.csx脚本中的命令行参数?_C#_C# Interactive_Csi - Fatal编程技术网

C# 如何访问.csx脚本中的命令行参数?

C# 如何访问.csx脚本中的命令行参数?,c#,c#-interactive,csi,C#,C# Interactive,Csi,我正在使用C#Interactive编译器运行.csx脚本。如何访问提供给脚本的任何命令行参数 csi script.csx 2000 如果您不熟悉csi.exe,下面是用法消息: >csi /? Microsoft (R) Visual C# Interactive Compiler version 1.3.1.60616 Copyright (C) Microsoft Corporation. All rights reserved. Usage: csi [option] ..

我正在使用C#Interactive编译器运行
.csx
脚本。如何访问提供给脚本的任何命令行参数

csi script.csx 2000

如果您不熟悉csi.exe,下面是用法消息:

>csi /?
Microsoft (R) Visual C# Interactive Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: csi [option] ... [script-file.csx] [script-argument] ...

Executes script-file.csx if specified, otherwise launches an interactive REPL (Read Eval Print Loop).

Environment.GetCommandLineArgs()
返回该示例的
[“csi”、“script.csx”、“2000”]

以下是我的脚本:

    var t = Environment.GetCommandLineArgs();
    foreach (var i in t)
        Console.WriteLine(i);
要将参数传入csx,请执行以下操作:

    scriptcs hello.csx -- arg1 arg2 argx
打印出:

    hello.csx
    --
    arg1
    arg2
    argx
csx和脚本参数之间的键是“--”。

CSI有一个。在大多数情况下,这将获得所需的参数,就像在C/C++程序中访问
argv
或在C#
Main()中访问
args
一样

Args
的类型为而不是
string[]
。因此,您将使用查找参数的数量,而不是
.Length

下面是一些示例用法:

#/usr/bin/env csi
Console.WriteLine($“有{Args.Count}Args:{string.Join(“,”,Args.Select(arg=>$”{arg}”))}”);
以及一些示例调用:

ohnob@DESKTOP-RC0QNSG MSYS ~/AppData/Local/Temp
$ ./blah.csx
There are 0 args:

ohnob@DESKTOP-RC0QNSG MSYS ~/AppData/Local/Temp
$ ./blah.csx hi, these are args.
There are 4 args: “hi,”, “these”, “are”, “args.”

ohnob@DESKTOP-RC0QNSG MSYS ~/AppData/Local/Temp
$ ./blah.csx 'hi, this is one arg.'
There are 1 args: “hi, this is one arg.”

的可能重复避免了试图猜测脚本的参数从何处开始的问题,因此我不建议使用本文中的方法answer@binki这应该是它自己的答案,而不是评论。@ChrisCharabaruk我已将其提升为答案并删除了我的评论。谢谢