简体   繁体   English

如何检查可执行文件是作为服务启动还是从SYSTEM帐户启动?

[英]How to check if executable started as service or from SYSTEM account?

I have a program that can be installed and started as service, or can be started from SYSTEM account using the 'at' command. 我有一个程序可以作为服务安装和启动,也可以使用“ at”命令从SYSTEM帐户启动。 I need to find a way to know if the executable was started as service or from SYSTEM account, so I could know if I need to call ServiceBase.Run or perform other work at the executable entry point (Main function). 我需要找到一种方法来了解可执行文件是作为服务启动还是从SYSTEM帐户启动,因此我可以知道是否需要调用ServiceBase.Run或在可执行文件入口点(Main函数)执行其他工作。

Environment.UserInteractive is false in both situations. 在两种情况下, Environment.UserInteractive均为false。

WindowsIdentity.GetCurrent().IsSystem is true in both situations. WindowsIdentity.GetCurrent().IsSystem在两种情况下都为true。

I'd like not to pass any command line arguments to solve this. 我不想通过任何命令行参数来解决这个问题。

Entry point's ReflectedType.BaseType.FullName doesn't work either. 入口点的ReflectedType.BaseType.FullName也不起作用。

You could try looking at the parent of your process. 您可以尝试查看过程的父级。

public static bool IsRunningAsService() 
{
    return Process.GetCurrentProcess().Parent().ProcessName == "services";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM