开发者

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜