Hey people, so here's the thing I've created a program that scans the WMI for any usb action and is triggers an event I then check from a list what USB event it was, completely applicatino specific. Anyway that code works perfectly!
Then I added code to hide the form on startup, by creating a NotifyIcon, and having the form default to Minimize. The Minimize event triggers and then the form is hidden and the icon in the system tray appears, a double click event on the icon brings back the form...... However, what I actually want is the Usb event to trigger the form re-appearing as well....But sadly I get some error, not sure how to solve it.
<pre lang="cs">public void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void Form1_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
}
WqlEventQuery weqQuery = new WqlEventQuery();
weqQuery.EventClassName = "__InstanceOperationEvent";
weqQuery.WithinInterval = new TimeSpan(0, 0, 3);
weqQuery.Condition = @"TargetInstance ISA 'Win32_PnPEntity'";
m_mewWatcher = new ManagementEventWatcher(weqQuery);
m_mewWatcher.EventArrived += new EventArrivedEventHandler(m_mewWatcher_EventArrived);
m_mewWatcher.Start();<pre>
<pre lang="cs">public void m_mewWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
bool bUSBEvent = false;
foreach (PropertyData pdData in e.NewEvent.Properties)
{
ManagementBaseObject mbo = (ManagementBaseObject)pdData.Value;
if (mbo != null)
{
foreach (PropertyData pdDataSub in mbo.Properties)
{
if (pdDataSub.Value == null)
{
}
else
{
namez = pdDataSub.Value.ToString();
if ( namez.Contains(" Electronics Dongle"))
{
position = namez.IndexOf("(COM");
position2 = namez.IndexOf(")");
bUSBEvent = true;
break;
}
}
}
if (bUSBEvent)
{
if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
{
if (dongle_found == false)
{
if ((position > 0))
if (SP.IsOpen)
{
SP.Close();
}
SP.PortName = namez.Substring(position + 1, position2 - (position + 1));
dongle_found = true;
}
Console.WriteLine("USB was plugged in");
File.AppendAllText(path, "bleh" + " Con");
string appendText = Environment.NewLine;
File.AppendAllText(path, appendText);
try
{
this.Show();
}
catch (InvalidCastException q)
{
Console.WriteLine("Caught: {0}", q);
}
notifyIcon1.Visible = false;
}
else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
{
if (SP.IsOpen)
{
SP.Close();
}
Console.WriteLine("USB was plugged out");
File.AppendAllText(path, "bleh" + "DC");
string appendText = Environment.NewLine;
File.AppendAllText(path, appendText);
dongle_found = false;
}
}
}
}
}
The problem comes in when I want to have the form re-appear, this.show() and the lines following throws a run time bug....
'BackGroundWorkers.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
I have no idea what is going on. Help!!!!!