检测在Windows XP下是否以管理员权限运行 [英] Detect if running with administrator privileges under Windows XP

查看:411
本文介绍了检测在Windows XP下是否以管理员权限运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何检测用户是否在Windows XP下运行管理员权限。这在Vista / Win7中很容易做到,因为 whoami 命令。下面是一个在Ruby中如何在Vista下做的代码片段:

I am trying to work out how to detect whether a user is running with admin rights under Windows XP. This is fairly easy to do in Vista/Win7 thanks to the whoami command. Here's a snippet in Ruby for how to do it under Vista:

注意,下面的链接现在包含了muteW建议的解决方案

Note, the following link now incorporates the solution suggested by muteW

http://gist.github.com/65931

麻烦的是,whoami没有Windows XP,所以上面的链接方法将总是返回false在WinXP,即使我们以管理员身份运行。

The trouble is, whoami doesn't come with Windows XP and so the above linked method will always return false on WinXP, even if we're running as an administrator.

所以,有没有人知道一种方法来检测我们是否在Windows XP下使用Ruby作为管理员运行,使用Ruby,命令行工具,批处理文件或甚至是第三方(需要开源,真的)工具?

So, does anyone know of a way to detect whether we're running as an admin under Windows XP using Ruby, command-line tools, batch-files, or even third-party (needs to be open source, really) tools?

推荐答案

这将检测用户是否在升级模式(例如,作为管理员运行的命令提示符)。它依赖于您需要管理员权限才能读取LOCAL SERVICE帐户注册表项:

This will detect if the user is running in elevated mode (eg a command prompt that was "Run As" Administrator). It relies on the fact that you require admin privileges to read the LOCAL SERVICE account reg key:

reg query "HKU\S-1-5-19"

如果无法读取,将返回非零错误代码,如果可以,则为零。

从XP向上起...

this will return a non-zero error code if it cannot be read, and zero if it can.
Works from XP up...

这篇关于检测在Windows XP下是否以管理员权限运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆