How to receive event when network is connected and also when user logs in

前端 未结 6 1442
再見小時候
再見小時候 2021-02-06 02:32

I have a service that runs and I\'d like to receive notification when:

a) the network is connected.

b) when a user logs in to the machine.

How can I do

相关标签:
6条回答
  • 2021-02-06 03:04
    //using Microsoft.Win32;
    //using System.Net.NetworkInformation;
    public class SessionChanges
    {
        public SessionChanges()
        {
            NetworkChange.NetworkAvailabilityChanged += 
                new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
            SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
        }
    
        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            if (e.Reason == SessionSwitchReason.SessionLogon)
            { 
                //user logged in
            }
        }
    
        void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
            { 
                //a network is available
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 03:07

    To be notified when any user logs in to the machine, call WTSRegisterSessionNotification, passing NOTIFY_FOR_ALL_SESSIONS, and listen for the WM_WTSSESSION_CHANGE message in the message loop.

    In the message, cast the wParam to the Microsoft.Win32.SessionSwitchReason enum to find out what happened, and pass the lParam to WTSQuerySessionInformation to find the username.

    [DllImport("Wtsapi32.dll", CharSet=.CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags); 
    
    //Pass this value in dwFlags
    public const int NOTIFY_FOR_ALL_SESSIONS          =  0x1; 
    
    //Listen for this message
    public const int WM_WTSSESSION_CHANGE = 0x02B1;
    
    //Call this method before exiting your program
    [DllImport("Wtsapi32.dll", CharSet=.CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd); 
    
    0 讨论(0)
  • 2021-02-06 03:11

    You can't do b) with .NET 2.0. For XP and before, you need to create a GINA replacement, which is an unmanaged DLL which exports a specified set of functions.

    In Vista, there is a different COM-based model which you have to use which you might be able to use .NET 2.0 for, but I wouldn't be surprised if you can't.

    What I would do is have your GINA replacement (or the equivalent on Vista) send a signal of some kind to your service.

    For a) these two links should be able to help:

    Network Awareness in Windows XP

    http://msdn.microsoft.com/en-us/library/ms700657(VS.85).aspx

    Network Awareness on Windows Vista

    http://msdn.microsoft.com/en-us/library/ms697388(VS.85).aspx

    0 讨论(0)
  • 2021-02-06 03:18

    SENS can do this. http://msdn.microsoft.com/en-us/magazine/cc301850.aspx

    Check for both ISensNetwork and ISensLogon.

    Adapt this for C# and you're done.

    0 讨论(0)
  • 2021-02-06 03:27

    You could write a WinLogin Notification package. However, I am not sure if it is possible to do in C#. (I have no reason to believe it isn't, but I have only ever done it in C++ and unmanaged code.)

    Here is a CodeProject link

    I don't know how to listen for when the network connects.

    0 讨论(0)
  • 2021-02-06 03:28

    To find out when the network is connected, add a handler for the NetworkAvailabilityChanged in the System.Net.NetworkInformation.NetworkChange class.

    One simple way to find out when a user logs in is to make a separate exe and put it into common startup. It would be executed whenever any user logs in, and could then communicate with your service and give the username. If you want your service to interact with the user's desktop, this is (I believe) the only way to do it. If you don't however, this might not be a good idea.

    Remember that it is possible for multiple users to be logged in at once, especially through Remote Desktop or Terminal Services (On Windows Server)

    0 讨论(0)
提交回复
热议问题