这个问题实际上是this one的副本。我想检测我的程序是通过UAC在Winows中提升权限,还是在Unix / Mono中以 root 运行。
我怎样才能在C#中做到这一点?
答案 0 :(得分:2)
类似下面的函数会处理问题的Unix / Mono结尾。顺便说一句,我实际上并没有编译或运行它,但你明白了。
private bool AmIRoot()
{
//Declarations:
string fileName = "blah.txt",
content = "";
//Execute shell command:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName = "whoami > " + fileName;
proc.StartInfo.Arguments = "";
proc.Start();
proc.WaitForExit();
//View results of command execution:
StreamReader sr = new StreamReader(fileName);
content = sr.ReadLine();
sr.Close();
//Clean up magic file:
File.Delete(fileName);
//Return to caller:
if(content == "root")
return true;
else
return false;
}