Link to home
Start Free TrialLog in
Avatar of fattumsdad
fattumsdad

asked on

Repair Network Connection Using C#

Good afternoon,

Normally, when our Wireless network cuts out, I right click on the wireless icon in the system tray and select "Repair".  Is there a way to accomplish this using C#?  Thanks!
Avatar of Yurich
Yurich
Flag of New Zealand image

there is, but you'll have to start your c# application and click repair on it, then it will move your mouse cursor to your systray, right-click the icon , and select "repair"...

if you want to automate it, you'll have to create a resident-program that would check the state of connection all the time and if it's not connected - see above.

regards,
yurich
Avatar of fattumsdad
fattumsdad

ASKER

Yurich,

How do I have the mouse cursor move to the systray?  That sounds interesting!
ASKER CERTIFIED SOLUTION
Avatar of Yurich
Yurich
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works great, and I now have the cursor jumping directly to the network icon...  however, I can't figure out how to simulate the right-click and then left-clicking "repair".
well, that one will be a bit more complicated...
you'll have to use api function since i don't know other ways of doing it.

using System.Runtime.InteropServices;

...

// somewhere in your class scope (namespace)
[ DllImport( "user32.dll" )]
private static extern void mouse_event( UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo );

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
private const UInt32 MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const UInt32 MOUSEEVENTF_RIGHTUP = 0x0010;

private void SendLeftClick()
{
      mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
      mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}

private void SendRightClick()
{
      mouse_event( MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, new System.IntPtr());
      mouse_event( MOUSEEVENTF_RIGHTUP, 0, 0, 0, new System.IntPtr());
}


// then, somewhere in the same function where you moved your button to the icon on the systray:
...
SendRightClick();
// pick the right values for ?? to move your cursor just slight above your appeared menu
Cursor.Position = new Point( Cursor.Position.X - ??, Cursor.Position.Y - ?? );
SendLeftClick();
...

well, that should be pretty much it,
good luck,
yurich