當以系統非管理員運行程序時,提升當前系統用戶權限

 1         /// <summary>
 2         /// 當系統非管理員運行程序時,提升當前系統用戶權限
 3         /// </summary>
 4         /// <param name="inUserName">系統管理員</param>
 5         /// <param name="inPassWord">管理員密碼</param>
 6         /// <param name="programName">待提升權限程序位置</param>
 7         /// <returns></returns>
 8         public bool LauchMontanaBrt(string inUserName, string inPassWord, string programName)
 9         {
10             /*
11              *使用系統runas命令行爲當前程序獲取管理員權限(更改運行程序的用戶帳戶)
12              * System.Diagnostics.Process cmd = System.Diagnostics.Process.Start("runas",
13              * "/user:[email protected]  \"C:\\Program Files\\Internet Explorer\\iexplore.exe\"");
14             */
15             //獲取當前系統用戶
16             WindowsIdentity ident = WindowsIdentity.GetCurrent();
17             //檢查當前用戶級別(管理員/普通用戶)
18             WindowsPrincipal principal = new WindowsPrincipal(ident);
19             if (principal.IsInRole(WindowsBuiltInRole.Administrator))
20             {
21                 //管理員用戶
22                 return true;
23             }
24             else
25             {
26                 try
27                 {
28                     Process MBRTProcess = new Process();
29                     MBRTProcess.StartInfo.UserName = inUserName;
30                     string strPWD = inPassWord;
31                     SecureString password = new SecureString();
32                     foreach (char c in strPWD.ToCharArray())
33                     {
34                         password.AppendChar(c);
35                     }
36                     MBRTProcess.StartInfo.Password = password;
37                     MBRTProcess.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
38                     MBRTProcess.StartInfo.FileName = programName;//textBox1.Text.Trim(); //"xxx.exe";
39                     //MBRTProcess.StartInfo.Arguments = "/run /wu";
40                     MBRTProcess.StartInfo.UseShellExecute = false;
41                     MBRTProcess.Start();
42                     //this.Hide();
43                     return true;
44                 }
45                 catch (Exception error)
46                 {
47                     MessageBox.Show(error.Message);
48                     return false;
49                 }
50             }
51         }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章