下面的函数将处理这个问题的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;
}