如何修复“;索引在数组“”的边界之外;例外

本文关键字:边界 例外 数组 何修复 索引 | 更新日期: 2023-09-27 18:28:54

我一直得到这个"Index was outside of the array"异常。我有一个带有文件名的数据表。网络表单页面上有一个列表框连接到桌面。我正在尝试从列表框中选择多个选项。然后将每个文件名发送到handler.ashx文件。正如你所看到的,我正在使用for循环。

这是main.aspx的代码:

namespace MultiImagesImageHandler
{
public partial class Main : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        int[] AllselectedIndex = ListBox1.GetSelectedIndices();
        int a = AllselectedIndex.Length;
        DataView tempDV = SqlDataSource1.Select(DataSourceSelectArguments.Empty) as DataView;
        for (int i = 0; i < a + 1; i++)
        {
            string fn = tempDV.Table.Rows[AllselectedIndex[i+1]].ItemArray[1].ToString();
            ImgHandler.filename[i] = fn.Trim();
        }
        Response.Redirect("image1.htm");
    }
}
}

handler.ashx.cs代码:

namespace MultiImagesImageHandler
{
/// <summary>
/// Summary description for ImgHandler
/// </summary>
public class ImgHandler : IHttpHandler
{
    public static string[] filename = new string[16];

    public void ProcessRequest(HttpContext context)
    {
        string saveTo = @"fileLocation" + filename[3];
        FileStream writeStream1 = new FileStream(saveTo, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        string loadFrom = @"fileLocation" + filename[3];
        using (FileStream fs1 = File.Open(loadFrom, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            ReadWriteStream(fs1, writeStream1);
        }
        byte[] tt = File.ReadAllBytes(context.Server.MapPath("~/images/" + filename[3]));
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(tt);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    // readStream is the stream you need to read
    // writeStream is the stream you want to write to
    private void ReadWriteStream(Stream readStream, Stream writeStream)
    {
        int Length = 256;
        Byte[] buffer = new Byte[Length];
        int bytesRead = readStream.Read(buffer, 0, Length);
        // write the required bytes
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = readStream.Read(buffer, 0, Length);
        }
        readStream.Close();
        writeStream.Close();
    }
}
}

我原以为这会用数据表中选定的文件名填充ImgHandler.ashx.cs中的数组,但事实并非如此。它不断提出这个例外。我不知道为什么这不起作用。

如何修复“;索引在数组“”的边界之外;例外

您有:

for (int i = 0; i < a + 1; i++)

应为:

for (int i = 0; i < a; i++)

我认为这是您的问题:

for (int i = 0; i < a + 1; i++)
{
    string fn = tempDV.Table.Rows[AllselectedIndex[i+1]].ItemArray[1].ToString();
    ImgHandler.filename[i] = fn.Trim();
}

将"a+1"更改为仅"a"

  string sRecontent = txtrepalcecontent.Text;
  string[] fileLineString = new string[sContent.Length];
  for (int i =0 ; i < sContent.Length; i++)
  {
      //fileLineString[0] = sContent[0].ToString();
      string[] content = sContent.Split(delim);
      if (sFind == content[i])  **//index was outside the bounds the array**
      {
          string A = sContent.Remove(i, txtfind.Text.Length);
          string S = A.Insert(i, sReplace);
          txtrepalcecontent.Text = S;
      }