C#.NET Getting User Name of machine using Windows Service

前端 未结 3 1507

I am having difficulty getting the User Name of a person logged into a machine using a windows service.

When using both System.Environment.UserName or WindowsIdentity.Ge

3条回答
  •  执笔经年
    2021-01-21 04:21

    For XP only, this advice should apply: Get Window Station for a non-interactive user per process, user or session?

    • Call OpenWindowStation to get a handle to "winsta0"
    • Call GetUserObjectInformation to find out who owns winsta0

    However, this approach will break when you upgrade your app to Vista, Windows 7 or above, where services run in a different session from interactive logons. You'll need to call the terminal services API to get a list of logged on users, and pick your 'interactive' one.

提交回复
热议问题