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!
LVL 1
fattumsdadAsked:
Who is Participating?
 
YurichCommented:
you can use a couple of api functions to get in there which would be the right way of doing it, but again, you can just assume that your icon is always at the same place (since it's not a commercial project but your own utility) and move it like (say when you press a button) this:

Rectangle rect = Screen.PrimaryScreen.Bounds;
Cursor.Position = new Point( rect.Width - 20, rect.Height - 20 );

in the case above it jumps exactly on my system day-time display...

regards,
yurich


0
 
YurichCommented:
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
0
 
fattumsdadAuthor Commented:
Yurich,

How do I have the mouse cursor move to the systray?  That sounds interesting!
0
 
fattumsdadAuthor Commented:
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".
0
 
YurichCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.