验证当前登录用户的权限(控制台程序)

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Diagnostics;
 6 
 7 namespace c2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             if (runcmd("net localgroup administrators |find \"%username%\"").IndexOf(System.Environment.UserName) >= 0)
14                 Console.WriteLine("Admin");
15             else
16                 Console.WriteLine("No Admin!");
17             Console.Read();
18         }
19         private static string runcmd(string command)
20         {
21             Process p = new Process();
22             p.StartInfo.FileName = "cmd.exe";           
23             p.StartInfo.Arguments = "/c " + command;    
24             p.StartInfo.UseShellExecute = false;        
25             p.StartInfo.RedirectStandardInput = true;   
26             p.StartInfo.RedirectStandardOutput = true;  
27             p.StartInfo.RedirectStandardError = true;   
28             p.StartInfo.CreateNoWindow = true;          
29             p.Start();   
30             return p.StandardOutput.ReadToEnd();   
31         }
32     }
33 }
34 
复制代码

net localgroup administrators |find \"%username%\"").IndexOf(System.Environment.UserName) >= 0

用这个命令也行

posted @   凡的世界  阅读(648)  评论(1编辑  收藏  举报
< 2008年9月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示