部署程序时System.ArgumentOutOfRangeException

目前,我正在开发一个可以在连接时检测USB设备的程序。 将该设备的所有文件和目录复制到指定的文件夹。 所有这些工作。 我build立这个程序,没有问题。 当我在我的Windows 7笔记本电脑上运行.exe(有一个分区)时,程序会执行它应该执行的操作。 当我在另一台Windows 7笔记本电脑(有两个分区)和一台Windows Vista笔记本电脑(有两个分区)上testing同一个程序时,我收到了这个错误消息(荷兰语):

System.ArgumentOutOfRangeException: De index valt buiten het bereik. Deze mag niet negatief zijn en moet kleiner zijn dan de grootte van de verzameling. Parameternaam: index bij System.ThrowHelper.ThrowArgumentOutOfRangeException() bij System.Collections.Generic.List`1.get_Item(Int32 index) bij PHL___USB_tool.USBTool.LoadDownloadItems() in C:\Users\2930682\Desktop\ONDERZOEK CopyFormatUSB\MyProgram\PHL - USB tool\PHL - USB tool\USB tool.cs:regel 162 bij PHL___USB_tool.USBTool.WndProc(Message& m) in C:\Users\2930682\Desktop\ONDERZOEK CopyFormatUSB\MyProgram\PHL - USB tool\PHL - USB tool\USB tool.cs:regel 70 bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) bij System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

如果我检查我的代码:第70行

编辑:这个function将被称为/接收Windows 7操作系统的消息。

  protected override void WndProc(ref Message m) { if (m.Msg == Native.WM_DEVICECHANGE) { if (m.WParam.ToInt32() == Native.DBT_DEVICEARRIVAL) { if (!_blnLoading) { switch (tabControl1.SelectedIndex) { case 0: SetProgress(_lstPictures); lblCopies.Visible = false; LoadDownloadItems(); break; case 1: SetProgress(_lstPictures2); LoadUploadItems(); break; case 2: SetProgress(_lstPictures3); LoadDeleteItems(); break; } } } else if (m.WParam.ToInt32() == Native.DBT_DEVICEREMOVECOMPLETE) { _blnLoading = false; _alreadyConnectedVolumes = null; _alreadyConnectedVolumes = new VolumeDeviceClass(); } } base.WndProc(ref m); } 

和我的代码162行

编辑:这个函数交叉检查当调用LoadDownloadItems()时程序以volumeDeviceClass.Devices开始时填充的LoadDownloadItems() 。 检查并select新添加的设备。 之后,检查它是否是具有IsUsbfunction的USB设备。

编辑:_lstPictures是三个图片框放在那里,当程序启动。

  private void LoadDownloadItems() { _blnLoading = true; lblErrorDestination.Visible = false; picErrorDestination.Visible = false; _intFilesCopied = 0; _intDirectoriesCopied = 0; VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass(); int position = -1; // need it for control, when to stop the for-loop for (int i = 0; i < volumeDeviceClass.Devices.Count; i++) { if (position != -1) break; else { string logicalDrive = ((Volume)volumeDeviceClass.Devices[i]).LogicalDrive; for (int j = 0; j < _alreadyConnectedVolumes.Devices.Count; j++) { if (((Volume)_alreadyConnectedVolumes.Devices[i]).LogicalDrive != logicalDrive) { position = i; break; } } } } // you don't need to check the position! // cause every new device during run-time, will be a removable device or usb-device if (position != -1 && volumeDeviceClass.Devices[position].IsUsb) { _connectedDevice = volumeDeviceClass.Devices[position]; _strLogicalDrive = ((Volume) _connectedDevice).LogicalDrive; _lstPictures[0].Image = Properties.Resources.Pass; lblFirst.Text = "Usb-device (" + _strLogicalDrive + @"\) found"; lblFirst.Refresh(); RefreshProgress(_lstPictures); if (_strDestination != null) { GetDirectories(_strLogicalDrive); _lstPictures[1].Image = Properties.Resources.Pass; _lstPictures[2].Image = Properties.Resources.Pass; RefreshProgress(_lstPictures); lblCopies.Visible = true; lblCopies.Text = "Files copied: " + _intFilesCopied + "\tDirectories copied: " + _intDirectoriesCopied; } else { _lstPictures[1].Image = Properties.Resources.Error; _lstPictures[2].Image = Properties.Resources.Error; RefreshProgress(_lstPictures); lblErrorDestination.Visible = true; picErrorDestination.Visible = true; } UsbEject(); _lstPictures[3].Image = Properties.Resources.Pass; RefreshProgress(_lstPictures); } } 

编辑:一些额外的信息再次testing我的程序在两个单独的笔记本电脑上。 笔记本电脑有相同的资源(相同的操作系统(Windows 7 Service Pack 1),两个惠普EliteBook 8530p,…)这是结果:

我的笔记本电脑(该程序是完美的):

_alreadyConnectedVolumes.Devices存在:

  • C:\ – >硬盘
  • D:\ – > DVD-rw-station

volumeDeviceClass.Devices存在:

  • C:\ – >硬盘
  • D:\ – > DVD-rw-station
  • E:\ – >我的USB驱动器 – >这是我可以做没有问题的行动!

我的伴侣的笔记本电脑(我得到了这个主题中显示的错误):

_alreadyConnectedVolumes.Devices存在:

  • C:\ – >硬盘(分区1 =主)
  • D:\ – >硬盘(分区2)
  • E:\ – > DVD-rw-station

volumeDeviceClass.Devices存在:

  • C:\ – >硬盘(分区1 =主)
  • D:\ – >硬盘(分区2)
  • E:\ – > DVD-rw-station
  • G:\ – >我的USB驱动器

在这里描述的两种情况下,我使用了相同的USB设备!

编辑:(解决?)这解决了我的问题,我想,我必须明天检查,如果它确切的工作。 但是现在这解决了嵌套循环的问题:

 for (int i = 0; i < _alreadyConnectedVolumes.Devices.Count; i++) { string logicalDrive = ((Volume)_alreadyConnectedVolumes.Devices[i]).LogicalDrive; for (int j = 0; j < volumeDeviceClass.Devices.Count; j++) { if (logicalDrive == ((Volume)volumeDeviceClass.Devices[j]).LogicalDrive) volumeDeviceClass.Devices.RemoveAt(j); } } 

在此之后,我只需要读出volumeDeviceClass.Devices是只是其中的一个项目! 导致我的progrem让你只注册在USB设备一次。

谁能告诉我是什么原因造成的错误。 原因不能想到一个,但可能是一个错误?

if (((Volume)_alreadyConnectedVolumes.Devices[i]).LogicalDrive != logicalDrive)你使用i作为索引,而不是j

这主要是猜测,但我想在某些情况下,您的position变量没有被设置。 尝试改变这一点:

 if (volumeDeviceClass.Devices[position].IsUsb) 

对此

 if (position != -1 && volumeDeviceClass.Devices[position].IsUsb) 

编辑 :同时确保_lstPictures有三个记录。