[英]Always run VS2015 debug with elevated privilege
I have a winform application with the following code in Program.cs
to make sure my app is always running with administrative privilege: 我在Program.cs
有一个带有以下代码的Winform应用程序,以确保我的应用程序始终以管理权限运行:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Init visual styles
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Check if app is launched with administrative privilage
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool administrativeMode = principal.IsInRole(WindowsBuiltInRole.Administrator);
// Check if application is already running
bool mutexCheck;
var mutex = new Mutex(true, Assembly.GetExecutingAssembly().GetType().GUID.ToString(), out mutexCheck);
if (!mutexCheck)
{
// Error
Utils.ShowError("Program is already running.");
return;
}
// If app is running without administrative privilage
if (!administrativeMode)
{
// Request administrative privilage
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Verb = "runas";
startInfo.FileName = Application.ExecutablePath;
try
{
// Run app as administrator
Process.Start(startInfo);
}
catch (Exception ex)
{
// Error
Utils.ShowError("Program failed to start - " + ex.Message);
return;
}
}
return;
// Launch application
Application.Run(new MyApp());
GC.KeepAlive(mutex);
}
}
Every time I press F5
to run the app in debug mode in Visual Studio 2015, the app appears to be just running - the debug environment is not launching. 每次我按F5
键在Visual Studio 2015中以调试模式运行该应用程序时,该应用程序似乎都在运行-调试环境未启动。
So, what I have to do is go to Debug -> Attach to Process...
and select MyApp.exe
and then I am prompted with this window: 因此,我要做的是转到MyApp.exe
Debug -> Attach to Process...
,然后选择MyApp.exe
,然后出现以下窗口提示我:
Then when I click on Restart under different credentials
- VS2015 restarts. 然后,当我单击Restart under different credentials
重新启动时-VS2015重新启动。 I will then have to close the already running instance of MyApp and then press F5
to re-launch with elevated privilege; 然后,我将不得不关闭已经运行的MyApp实例,然后按F5
键以提升的特权重新启动; when I do - the debug works. 当我这样做时-调试有效。
Is there a way to set my VS2015 to always run this project with elevated privilege? 有没有办法将我的VS2015设置为始终以提升的特权运行该项目?
Try running your Visual Studio as Administrator. 尝试以管理员身份运行Visual Studio。 In the cases that I faced with insufficient privileges Running Visual Studio As Administrator has fixed my problem. 在遇到特权不足的情况下,以管理员身份运行Visual Studio已解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.