示例#1
0
        /// <summary>
        /// Internally handle state changes and notify listeners.
        /// </summary>
        /// <param name="e"></param>

        private void DoStateChanged(UsbStateChangedEventArgs e)
        {
            if (handler != null)
            {
                UsbDisk disk = e.Disk;

                // we can only interrogate drives that are added...
                // cannot see something that is no longer there!

                if ((e.State == UsbStateChange.Added) && (e.Disk.Name[0] != '?'))
                {
                    // the following Begin/End invokes looks strange but are required
                    // to resolve a "DisconnectedContext was detected" exception which
                    // occurs when the current thread terminates before the WMI queries
                    // can complete.  I'm not exactly sure why that would happen...

                    GetDiskInformationDelegate gdi = new GetDiskInformationDelegate(GetDiskInformation);
                    IAsyncResult result            = gdi.BeginInvoke(e.Disk, null, null);
                    gdi.EndInvoke(result);
                }

                handler(e);
            }
        }
示例#2
0
        private void DoStateChanged(UsbStateChangedEventArgs e)
        {
            if (e.State == UsbStateChange.Added)
            {
                Drive addedDrive = new Drive("?", "", "Unknown drive", true, false);
                // Managing lists and naming
                string infoLabel = GetDriveInfo(e.Disk.Name + "\\");
                var    drive     = drivesList.Where(p => (String.Equals(p.Label, e.Disk.ToString(), StringComparison.CurrentCulture) || String.Equals(p.Label, infoLabel, StringComparison.CurrentCulture)));

                if (drive.Count() > 0) // if it is in the list
                {
                    foreach (Drive d in drive)
                    {
                        d.Connected = true;
                        addedDrive  = d;
                    }
                    //drivesList.ElementAt(drivesList.IndexOf(drive.ToList().First())).Connected = true;
                    //addedDrive = drivesList.ElementAt(drivesList.IndexOf(drive.ToList().First()));
                }
                else // if it isn't in the list
                {
                    string label;
                    if (e.Disk.ToString().Length > infoLabel.Length)
                    {
                        label = e.Disk.ToString();
                    }
                    else
                    {
                        label = infoLabel;
                    }
                    addedDrive = new Drive(e.Disk.Name[0].ToString(), e.Disk.Name + "\\", label, true, false);
                    drivesList.Add(addedDrive);
                    myDevicesGrid.Rows.Add(false, label);
                    //System.IO.File.AppendAllText(knownDrivesFile, label + NL);
                }
                if (monitoring)
                {
                    ProcessDrive(addedDrive);
                }
                // check for any additional drives that might have came with this one
                AddRemovebleDrives(monitoring);
                //DumpDrivesInfo();
            }
            else if (e.State == UsbStateChange.Removed)
            {
                //DumpDrivesInfo();

                //System.IO.File.AppendAllText(coutFile, NL + rec.ToString()); // check if null
                // Check by notification
                var connectedDrives     = drivesList.Where(p => p.Connected);
                var existantDrivesRoots = GetRemovableDrivesRoots();
                foreach (Drive drive in connectedDrives)
                {
                    if (!existantDrivesRoots.Contains(drive.RootDirectory))
                    {
                        drivesList.ElementAt(drivesList.IndexOf(drive)).Connected = false;
                    }
                }
                //DumpDrivesInfo();
            }
        }