Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems when plugged 2 devices at the same time. #5

Open
yaks77 opened this issue Oct 16, 2016 · 0 comments
Open

Problems when plugged 2 devices at the same time. #5

yaks77 opened this issue Oct 16, 2016 · 0 comments

Comments

@yaks77
Copy link

yaks77 commented Oct 16, 2016

I am developing a tool for my company which analyses every USB device plugged to the system and if it doesn't fulfill some criteria , it is ejected automatically.
Doing some search on internet I ended with two little projects which do exactly what I need.

I just integrated both in a single .NET solution and I simply add an eject command every time I plugged a new USB device.

private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
        {
            // Report the event in the listbox.
            // e.Drive is the drive letter for the device which just arrived, e.g. "E:\\"
            string s = "Drive arrived " + e.Drive;
            listBox1.Items.Add(s);

            VolumeDeviceClass volumes = new VolumeDeviceClass();
            foreach (Volume vol in volumes.Devices) {
                if (vol.LogicalDrive.Equals(e.Drive.Replace("\\","")))
                {
                    listBox1.Items.Add("Attempting to eject drive: " + e.Drive);
                    vol.Eject(false);
                    listBox1.Items.Add("Done ejecting drive.");
                    break;
                }
            }
}

My problem appears when I plug 2 devices at the same time, the code only manages to eject the first device. It seems like that the ejection of the first USB drive also remove the "onArrrived" event of the second drive. As a result, the second drive keeps accessible.
Any idea of what might be happening?

I attach the .net solution in case you want to reproduce the issue.

SimpleDetector.zip

Many thanks in advance.
Jose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant