目前我正在开发一个程序,该程序可以在连接时检测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设备。
谁能告诉我是什么原因导致了错误。原因不能想到一个,但可能是一个错误?
你可以检查'_alreadyConnectedVolumes.Devices [i]'这里设备的数量可能少于'我',你可以检查'我'和'j','我'不应该大于'j' – 2012-01-18 17:15:52
@AmarPalsapure程序在我的笔记本电脑上启动时'_alreadyConnectedVolumes.Devices [i]'包含C:\,D:\和E:\驱动器。当'volumeDeviceClass.Devices'被填充时,它包含C:\,D:\,E:\和F:\驱动器,最后一个是我想要使用的newley连接的USB驱动器 – 2012-01-18 18:50:27