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

前端 未结 6 1455
再見小時候
再見小時候 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
            }
        }
    }
    

提交回复
热议问题