2012-01-18 68 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