Windows 中哪个进程是用户特定的?
我想知道 Windows 中的哪个进程是特定于用户的,我的意思是它是为每个用户登录创建的。我尝试了 explorer.exe,但是当您切换用户并登录新帐户时,它会在我的代码中显示旧的登录名。基本上我只需要记录在应用程序中登录的用户。
i wanted to know which process in Windows is user specific, i mean it get created for each user login. i tried explorer.exe but when u switch user and log into new account then it shows old login name in my code. basically i need to just log which user logging when in app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要知道哪些用户正在使用您的应用,您可以在启动应用时仅检查
Environment.UserName
吗?If all you need to know is which user(s) are using your app, can you just check
Environment.UserName
when you start your app?我错过了表明您创建了 Windows 服务的标签。这是一种与常规应用程序截然不同的动物类型,您收到的针对其中一种动物的建议不一定适用于另一种动物。
具体来说,我注意到您已将此问题标记为
windows-7
。如果您尝试在 Windows 7 下运行此服务,则需要了解有关从 Windows Vista 开始如何大幅更改 Windows 服务模型的一些知识。具体来说,它们现在在隔离会话中运行并且被禁止直接与用户交互。另请参阅
基本点是,从 Windows 服务的角度来看,不存在当前登录用户这样的概念。 Windows 服务在其自己的隔离会话中运行,不隶属于任何特定用户。这就是为什么您找到的代码确定与特定进程关联的用户未按照您对 Windows 服务的预期工作。标准用户不拥有运行该服务的进程。 (考虑到我对您的要求的理解,用应用程序替换您的服务也不是一个可行的选择。正如我在这里解释的,用户模式应用程序在特定用户登录时启动,并在该用户注销时关闭。)
另一个问题是多个用户可以同时登录到一台工作站。 Windows 是一个彻底的多用户操作系统,因此您最好的希望就是枚举当前登录的所有用户。
NetWkstaUserEnum
函数 将为您提供列表,但请注意,它包括所有类型的登录,包括交互式用户、服务和批量登录。要从 C# 调用此函数,您需要 P/Invoke — 您可以在 pinvoke.net。I missed the tag indicating you created a Windows Service. That's a very different type of animal than a regular application, and the advice you receive for one is not necessarily transferable to the other.
Specifically, I notice that you've tagged this question
windows-7
. If you're trying to run this service under Windows 7, you need to understand a few things about how the model for Windows Services was substantially altered starting with Windows Vista. Specifically, they now run in an isolated session and are prohibited from interacting directly with the user.Also see my answer here for a better explanation.
The fundamental point is that, from the perspective of a Windows Service, there is no such concept as the currently logged-on user. A Windows Service runs in its own isolated session and is not affiliated with any particular user. That's why the code you found to determine the user associated with a particular process is not working as you expect for a Windows Service. A standard user doesn't own the process running the service. (And replacing your service with an application is also not a viable option, given how I understand your requirements. As I explain here, user-mode applications are started when a particular user logs on and will be closed whenever that user logs off.)
Another problem is that more than one user can be logged in simultaneously to a single workstation. Windows is a thoroughly multi-user operating system, so the best that you can hope for is to enumerate all of the currently logged in users. The
NetWkstaUserEnum
function will get you that list, but note that it includes all types of logons, including interactive users, services, and batch logons. To call this function from C#, you will need to P/Invoke—you can find information about that over on pinvoke.net.