You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.1 KiB
92 lines
3.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using System.ServiceModel;
|
|
using System.Net;
|
|
using System.Diagnostics;
|
|
|
|
namespace wcfControlMonitorClient
|
|
{
|
|
static class Program
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// 应用程序的主入口点。
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
|
|
//if (!IsRunningAsAdmin())
|
|
//{
|
|
// RestartAsAdmin();
|
|
// return;
|
|
//}
|
|
//Application.Run(new FrmLogin());
|
|
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
/**
|
|
* 当前用户是管理员的时候,直接启动应用程序
|
|
* 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
|
|
*/
|
|
//获得当前登录的Windows用户标示
|
|
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
|
|
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
|
|
//判断当前登录用户是否为管理员
|
|
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
|
|
{
|
|
//如果是管理员,则直接运行
|
|
Application.Run(new FrmLogin());
|
|
}
|
|
else
|
|
{
|
|
//创建启动对象
|
|
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
|
|
startInfo.UseShellExecute = true;
|
|
startInfo.WorkingDirectory = Environment.CurrentDirectory;
|
|
startInfo.FileName = Application.ExecutablePath;
|
|
//设置启动动作,确保以管理员身份运行
|
|
startInfo.Verb = "runas";
|
|
try
|
|
{
|
|
System.Diagnostics.Process.Start(startInfo);
|
|
}
|
|
catch
|
|
{
|
|
return;
|
|
}
|
|
//退出
|
|
Application.Exit();
|
|
}
|
|
|
|
}
|
|
|
|
static bool IsRunningAsAdmin()
|
|
{
|
|
var identity = System.Security.Principal.WindowsIdentity.GetCurrent();
|
|
var principal = new System.Security.Principal.WindowsPrincipal(identity);
|
|
return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
|
|
}
|
|
|
|
static void RestartAsAdmin()
|
|
{
|
|
var startInfo = new ProcessStartInfo();
|
|
startInfo.FileName = Application.ExecutablePath;
|
|
startInfo.Verb = "runas";
|
|
//以管婴员身货运行
|
|
try
|
|
{
|
|
Process.Start(startInfo);
|
|
}
|
|
catch (System.ComponentModel.Win32Exception)
|
|
{
|
|
//I1用户取将了管理员权跟提示,或者其他错误11 可以在此处处塑错误情况}
|
|
}
|
|
Application.Exit();
|
|
}
|
|
}
|
|
}
|