簡體   English   中英

訪問DateTime對象的列表列表時發生異常

[英]Exception while accessing a List of List of DateTime objects

我有一個List<List<DateTime>> ,我正嘗試通過一個for循環設置它,但是我得到了IndexOutOfRangeException ,它努力了但不能消除該異常。

代碼如下:

protected void btnTimeStamp_Click(object sender, EventArgs e)
        {
            string serviceName = DropDownList1.SelectedItem.Text;
            List<string> serviceNameArray = new List<string>();
            for (int x = 1; x <= 40; x++)
            {
                if (x < 10)
                {
                    serviceNameArray.Add(@"\\Server10" + x + @"\WebSite" + "\\" + serviceName);
                }
                else
                {
                    serviceNameArray.Add(@"\\Server1" + x + @"\WebSite" + "\\" + serviceName);
                }
            }
            List<List<string>> ultimateList = new List<List<string>>();
            List<int> numList = new List<int>();
            //Call the Search method

            for (int y = 0; y < 40; y++)
            {
                ultimateList.Add(Search(serviceNameArray[y]));
            }
            for (int z = 0; z < 40; z++)
            {
                numList.Add(ultimateList[z].Count);
            }
            List<List<DateTime>> listOfDateTimes = new List<List<DateTime>>();
            for(int xx=0;xx<40;xx++)
               {
                for(int yy=0;yy<numList[xx];yy++)
                {
                **listOfDateTimes[xx].Add(File.GetLastWriteTime(ultimateList[xx][yy]));//Index out of range exception on this line of code**
                }
            }
           //Some more code to create a dynamic data table and stuffs
}

public List<string> Search(string path)
        {
            List<string> listOfFiles = new List<string>();
            try
            {
                foreach (string files in Directory.GetFiles(path))
                {
                    listOfFiles.Add(files);
                }
                foreach (string dirs in Directory.GetDirectories(path))
                {
                    listOfFiles.AddRange(Search(dirs));
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            return listOfFiles;

        }

代碼摘要如此,我需要獲取約40個共享位置的文件名及其上次寫入時間,並將它們綁定在網格中。

因此,我一直在使用List<List<string>>List<List<DateTime>>

高手請幫忙。

問候

阿努拉格

您沒有初始化內部列表,請使用以下行初始化內部列表:

listOfDateTimes.Add(new List<DateTime>());

相關代碼部分:

List<List<DateTime>> listOfDateTimes = new List<List<DateTime>>();           

for(int xx=0;xx<40;xx++)
{
    listOfDateTimes.Add(new List<DateTime>());
    for(int yy=0;yy<numList[xx];yy++)
    {
        listOfDateTimes[xx].Add(File.GetLastWriteTime(ultimateList[xx][yy]));
    }
}

如果異常仍然存在,問題似乎與ultimateList有關; 檢查的最大指數ultimateList

listOfDateTimes為空,您忘記插入嵌套列表。

List<List<DateTime>> listOfDateTimes = new List<List<DateTime>>();
for(int xx=0;xx<40;xx++)
{
    listOfDateTimes.Add(new List<DateTime>());
    for(int yy=0;yy<numList[xx];yy++)
    {
        listOfDateTimes[xx].Add(File.GetLastWriteTime(ultimateList[xx][yy]));
    }
}

要么

List<List<DateTime>> listOfDateTimes = Enumerable.Range(0, 40)
                                                 .Select(i => new List<DateTime>())
                                                 .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM