c determines logged in user permission code sharing by calling cmd by the process

  • 2020-05-30 20:59:09
  • OfStack


/// <summary>
///  The application's main entry point. 
/// </summary>
[STAThread]
static void Main()
{
  if (RunCmd("net localgroup administrators").IndexOf(System.Environment.UserName) >= 0)
    {
    // Smooth execution. 
    }
    else
    {
        // Error prompt system is not the administrator user login, easy to cause the program crash. Please log out with administrator privileges. 

                
        // And exit the program. 
     }
}
/// <summary>
///  call cmd.exe The program executes the command. 
/// </summary>
/// <param name="command"> The command to be executed </param>
/// <returns></returns>
static string RunCmd(string command)
{
    // The instance 1 a Process Class, start 1 Independent processes 
    Process p = new Process();
    //Process Class has 1 a StartInfo Property, this is ProcessStartInfo Class, including 1 Some properties and methods, we use the following properties: 
    p.StartInfo.FileName = "cmd.exe";           // Program name 
    p.StartInfo.Arguments = "/c " + command;    // Set program execution parameters 
    p.StartInfo.UseShellExecute = false;        // Shut down Shell The use of 
    p.StartInfo.RedirectStandardInput = true;   // Redirect standard input 
    p.StartInfo.RedirectStandardOutput = true;  // Redirect standard output 
    p.StartInfo.RedirectStandardError = true;   // Redirect error output 
    p.StartInfo.CreateNoWindow = true;          // Set not to display a window 
    p.Start();   // Start the 
    p.StandardInput.WriteLine("exit");        // But remember to add Exit Otherwise the 1 It will crash when the stroke is executed 
    return p.StandardOutput.ReadToEnd();        // Get command execution results from the output stream 
}


Related articles: