2012-01-18 69 views
2

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

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) 

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

编辑:此函数将被调用/接收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

编辑:此功能交叉检查程序与volumeDeviceClass.Devices启动时,当LoadDownloadItems()被称为是充满_alreadyConnectedVolumes.Devices。检查并选择新添加的设备。之后检查它是否是具有IsUsb功能的USB设备。

编辑:_lstPictures是在程序启动时放在那里的三个Picturebox。

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); 
     } 
    } 

编辑:一些额外的信息在两个不同的笔记本电脑再次测试我的程序。该系列笔记本电脑具有相同的资源(相同的运行系统正(Windows 7的服务包1),无论惠普EliteBook 8530p,...)这是结果:

我的笔记本电脑(分别是程序完美的作品):

_alreadyConnectedVolumes.Devices存在的:

  • C:\ - >硬盘
  • d:\ - > DVD-RW-站

volumeDeviceClass.Devices的存在:

  • C:\ - >硬盘
  • d:\ - > DVD-RW站
  • E:\ - >我的USB驱动器 - >这是一个人,我可以做没有问题的行动!

我的伴侣的膝上型电脑(分别为我得到本主题中所示的错误):

_alreadyConnectedVolumes.Devices存在的:

  • C:\ - >硬盘(分区1 =主)
  • d:\ - >硬盘(分区2)
  • E:\ - > DVD-RW-站

volumeDeviceClass.Devices存在的:

  • C:\ - >硬盘(分区1 =主)
  • d:\ - >硬盘(分区2)
  • E:\ - > DVD- RW-站
  • 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设备。

谁能告诉我是什么原因导致了错误。原因不能想到一个,但可能是一个错误?

+1

你可以检查'_alreadyConnectedVolumes.Devices [i]'这里设备的数量可能少于'我',你可以检查'我'和'j','我'不应该大于'j' – 2012-01-18 17:15:52

+0

@AmarPalsapure程序在我的笔记本电脑上启动时'_alreadyConnectedVolumes.Devices [i]'包含C:\,D:\和E:\驱动器。当'volumeDeviceClass.Devices'被填充时,它包含C:\,D:\,E:\和F:\驱动器,最后一个是我想要使用的newley连接的USB驱动器 – 2012-01-18 18:50:27

回答

0

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

if (volumeDeviceClass.Devices[position].IsUsb) 

这个

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

编辑:也确保_lstPictures有三个记录它。

+1

感谢Matt提供的快速回复...我会尝试并发布结果。 在Onload中,它总是有三个在_lstPictures中的recrods。 – 2012-01-18 15:22:31

+0

尝试了马特建议的解决方案。没有工作,仍然是一样的错误。 使用调试器进行检查,'_lstPictures'被填充并且有三个PictureBox。 更多信息: 当插入新设备时,会调用LoadDownloadItems()。在'_alreadyConnectedVolumes'中,连接的设备和硬盘在程序启动时存储。所以当调用LoadDownloadItems()时,我会填充'volumeDeviceClass.Devices'。我交叉检查这两个,检查哪个设备是新的,然后检查它是否是USB设备。 欢迎其他想法或解决方案来解决这个问题。 Thanks Martijn – 2012-01-18 15:48:31

+0

'Devices'的类型是什么? – 2012-01-18 18:14:16

1

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

+0

是的,也有人注意到msdn论坛。但没有解决问题。但是通过注意到这个问题,我发现呃总是出现了for循环错误...试图解决这个问题,让所有人都发帖 – 2012-01-18 21:09:35

+1

for循环并没有完全希望他们应该这样做。对于只有一个parition的电脑,这不是一个问题。对于多个分区它给出了错误,这段代码解决了我的问题是在我原来的问题! – 2012-01-18 21:18:00