C#: detecting if running as super user, both in Windows and Mono
this qu开发者_运维问答estion is actually a duplicate of this one. I want to detect if my program is being run either with privilege elevation in Winows through UAC, or as root in Unix/Mono.
How can I do that in C#?
Something like the function below would take care of the Unix/Mono end of the question. Btw, I didn't actually compile or run this, but you get the idea.
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;
}
精彩评论