Fab*_*Fab 5 c# console batch-file console-application command-line-arguments
我已经创建了一个可以读取命令参数的控制台应用程序(使用Visual Studio 2010).
当我调试时,我解析一些在Project-> [project name] Properties ... - > Debug - > Command line arguments中设置的测试参数:
它读作:"parametername1 | parametervalue1""parametername2 | parametervalue2""parametername3 | parametervalue3"
我使用以下代码来读取参数:
for (Int16 argumentsCount = 0; argumentsCount < args.Length; argumentsCount++)
{
String[] parameterItem = args[argumentsCount].Split('|');
String parameterName = parameterItem[0].ToString();
String parameterValue = parameterItem[1].ToString();
/*code continues*/
}
Run Code Online (Sandbox Code Playgroud)
当我在调试模式下运行应用程序时它工作得很好并且所有参数都被读取.
然后我将应用程序发布到服务器并确保它安装了正确的权限(为了演示的目的,我们说它在C:\ MyApp上,并且Complied代码驻留在MyApp.application中)
然后我创建了一个执行应用程序的批处理脚本.*.BAT包含以下命令:
"C:\MyApp\MyApp.application" "parametername1|parametervalue1" "parametername2|parametervalue2" "parametername3|parametervalue3"
Run Code Online (Sandbox Code Playgroud)
当我运行批处理时,这种工作就像应用程序一样...但是......我的应用程序没有收到任何参数.我知道这是因为我重新编译并发布了一些代码来读取接收的参数数量:
Console.Write("Arguments " + args.Length.ToString());
Run Code Online (Sandbox Code Playgroud)
并显示参数:0
有人可以告诉我如何编写我的批处理脚本来运行应用程序并解析我的参数/命令行参数.
预计到达时间:没关系。你的问题.application不是.exe. 查看您的文件关联与.application以下内容相比发生了什么.exe:
> assoc .application
.application=Application.Manifest
> ftype Application.Manifest
Application.Manifest=rundll32.exe dfshim.dll,ShOpenVerbApplication %1
> assoc .exe
.exe=exefile
> ftype exefile
exefile="%1" %*
Run Code Online (Sandbox Code Playgroud)
您看到那里传递的内容有何不同吗?即普通可执行文件获取命令行参数(%*)。所以我想你应该使用可执行文件而不是可执行清单或任何.application实际的东西(老实说,我从未在野外见过它)。
使用相当少的测试程序
class Args {
static void Main(string[] args) {
for (int i = 0; i < args.Length; i++) {
System.Console.WriteLine("[{0}]=<{1}>", i, args[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说可以。以下批处理文件:
@"args.exe" "parametername1|parametervalue1" "parametername2|parametervalue2" "parametername3|parametervalue3"
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
[0]=<parametername1|parametervalue1>
[1]=<parametername2|parametervalue2>
[2]=<parametername3|parametervalue3>
Run Code Online (Sandbox Code Playgroud)
所以我猜你没有向我们展示的代码中有问题。也许您实际上并未在 C# 应用程序中使用命令行参数,而是引用了不同的命令行参数string[]?