【发布时间】:2025-12-31 08:10:11
【问题描述】:
可能已经有类似的问题了,但我想我这里有一个具体案例
我正在尝试了解现有应用程序的工作方式并立即对其进行调试
这是一个控制台应用程序,我提供了 6 个参数:
- 职位名称
- 布尔标志
- 文件位置
- 文件类型
- 日期
- 文件应用 ID
我通过项目“调试”部分下的命令行参数值提供参数列表
所以,它看起来像这样:"MyJobName" "0" "C:\\MyFile.txt" "MyFileType" "20200318" "MyAppID"
应用程序有以下逻辑:
SortedList<string, string> L = new SortedList<string,string>();
for (int i = 2; i <= args.GetLength(0) - 1; i++)
{
L.Add(args[i].Split(':')[0], args[i].Split(':')[1]);
}
List<SqlParameter> p = new List<SqlParameter>();
p.Add(new SqlParameter("@JobName", args[0]));
string xmlResult = D.RunProcedureXmlString("SPU_GetJobInfo", p);
所以,当我在第一次迭代中点击循环内的行时,会发生以下运行时错误:
args[i].Split(':')[1] 'args[i].Split(':')[1]' threw an exception of type 'System.IndexOutOfRangeException' string {System.IndexOutOfRangeException}
现有逻辑有什么问题,解决办法是什么?
我不确定修复是否会破坏我之前猜测的工作。稍后需要对其进行测试。
【问题讨论】:
-
嗯...您确定 arg 中的所有项目都有
:分隔符吗? -
既然
for (int i = 2; i < args.Length; i++)更容易阅读,为什么还要写这么复杂的for (int i = 2; i <= args.GetLength(0) - 1; i++)? -
为什么从2开始?第一项在
i=0。 -
@JAlex - 第一项实际上在 1。因为 0 是 arg 列表中的可执行文件名称。
-
@LegacyCode - 哦,谢谢。
标签: c# console-application indexoutofrangeexception