.....
你看下 fileNames[i+1].Substring(17,2)) 结果是什么先

解决方案 »

  1.   

    行 75: int fileNamesNum1 = Convert.ToInt32(fileNames[i+1].Substring(17,2));行 76:把断点设在这里,看看什么错误!
      

  2.   

    不怎么会设断点查看,我把那行设为断点了,怎么查看fileNames[i+1].Substring(17,2))的结果啊?
      

  3.   

    你确定 fileNames 的长度超过了19么?如果fileNames长度少于19的话,这里就肯定会出现异常。
      

  4.   

    鼠标将焦点设在那一行:76行,然后按F9,F5进行调试,到了那一步的时候查看fileNames[i+1].Substring(17,2)的值
      

  5.   

    在Substring前一定要判断一下长度1、看下str.Substring()越界2、看一下你的取值是否能转化成Int   应该是不行,FormatException转化异常时触发
      

  6.   

    fileNames[i]都是这样的文件:"E:\001\201004271110.xls",文件名不同而已;
     for (int i = 0; i < fileNames.Length - 1; i++)
            {
                int filePathNum = Convert.ToInt32(filePath.Substring(17, 2));
                int fileNamesNum = Convert.ToInt32(fileNames[i].Substring(17,2));
                int fileNamesNum1 = Convert.ToInt32(fileNames[i+1].Substring(17,2));
            }
    str.Substring()不会越界吧,而且肯定是能转换为Int的,具体的代码是这样的:  protected void btnOK_Click(object sender, EventArgs e)
        {
            string dir = @"E:\";
            string monitorID = DropDownListMonitor.SelectedValue;
            string pathMonitor = dir + monitorID;
            string[] fileNames = Directory.GetFiles(pathMonitor);
            string filePath = getFilePath();            for (int i = 0; i < fileNames.Length - 1; i++)
                {
                    if (filePath == fileNames[i])
                    {
                        lblFilePath.Text = "您查找的文件为:" + filePath.Substring(7, 16);
                        break;
                    }
                    else if ((filePath != fileNames[i]) && (filePath.Substring(0, 17) == fileNames[i].Substring(0, 17)))
                    {
                        int filePathNum = Convert.ToInt32(filePath.Substring(17, 2));
                        int fileNamesNum = Convert.ToInt32(fileNames[i].Substring(17, 2));
                        int fileNamesNum1 = Convert.ToInt32(fileNames[i + 1].Substring(17, 2));                    if (Math.Abs(filePathNum - fileNamesNum) < Math.Abs(filePathNum - fileNamesNum1))
                        {
                            lblFilePath.Text = "您查找的文件不存在,这个是最接近的文件:" + fileNames[i].Substring(7, 16);
                            break;
                        }
                    }
                    else
                    {
                        lblBack.Text = "没有符合条件的文件,请继续查找。";
                    }
                }
            }我把它放在下面,就不会报错。