C# 如何在网络连接和用户登录时接收事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/687156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:30:32  来源:igfitidea点击:

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

c#.neteventsnetworking

提问by Rory

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

我有一项正在运行的服务,我希望在以下情况下收到通知:

a) the network is connected.

a) 网络已连接。

b) when a user logs in to the machine.

b) 当用户登录机器时。

How can I do this? (C# .NET 2.0)

我怎样才能做到这一点?(C# .NET 2.0)

采纳答案by Timothy Carter

//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
        }
    }
}

回答by Khadaji

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.)

您可以编写一个WinLogin 通知包。但是,我不确定是否可以在 C# 中进行。(我没有理由相信它不是,但我只在 C++ 和非托管代码中做过。)

Here is a CodeProject link

这是一个 CodeProject链接

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

我不知道如何监听网络何时连接。

回答by David

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

SENS 可以做到这一点。 http://msdn.microsoft.com/en-us/magazine/cc301850.aspx

Check for both ISensNetwork and ISensLogon.

检查 ISensNetwork 和 ISensLogon。

Adapt this for C# and you're done.

为 C# 调整它,你就完成了。

回答by casperOne

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.

你不能用 .NET 2.0 做 b)。对于 XP 及之前版本,您需要创建一个 GINA 替代品,它是一个非托管 DLL,它导出一组指定的函数。

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

在 Vista 中,有一个不同的基于 COM 的模型,您必须使用它,您可能可以使用 .NET 2.0,但如果您不能使用,我不会感到惊讶。

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

我要做的是让您的 GINA 替代品(或 Vista 上的等效品)向您的服务发送某种信号。

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

对于 a) 这两个链接应该能够提供帮助:

Network Awareness in Windows XP

Windows XP 中的网络感知

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

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

Network Awareness on Windows Vista

Windows Vista 上的网络感知

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

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

回答by SLaks

To find out when the network is connected, add a handler for the NetworkAvailabilityChangedin the System.Net.NetworkInformation.NetworkChangeclass.

要找出网络何时连接,请NetworkAvailabilityChangedSystem.Net.NetworkInformation.NetworkChange类中添加一个处理程序。

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.

找出用户何时登录的一种简单方法是制作一个单独的 exe 并将其放入公共启动中。它会在任何用户登录时执行,然后可以与您的服务通信并提供用户名。如果您希望您的服务与用户的桌面交互,这是(我相信)唯一的方法。但是,如果您不这样做,这可能不是一个好主意。

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

请记住,多个用户可以同时登录,尤其是通过远程桌面或终端服务(在 Windows 服务器上)

回答by SLaks

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

要在任何用户登录机器时收到通知,请在消息循环中调用WTSRegisterSessionNotification、传递NOTIFY_FOR_ALL_SESSIONS和侦听WM_WTSSESSION_CHANGE消息。

In the message, cast the wParamto the Microsoft.Win32.SessionSwitchReasonenum to find out what happened, and pass the lParamto WTSQuerySessionInformationto find the username.

在消息中,将 投射wParamMicrosoft.Win32.SessionSwitchReason枚举以找出发生了什么,并传递lParamWTSQuerySessionInformation以查找用户名。

[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);