[英]Always run VS2015 debug with elevated 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);
}
}
每次我按F5
鍵在Visual Studio 2015中以調試模式運行該應用程序時,該應用程序似乎都在運行-調試環境未啟動。
因此,我要做的是轉到MyApp.exe
Debug -> Attach to Process...
,然后選擇MyApp.exe
,然后出現以下窗口提示我:
然后,當我單擊Restart under different credentials
重新啟動時-VS2015重新啟動。 然后,我將不得不關閉已經運行的MyApp實例,然后按F5
鍵以提升的特權重新啟動; 當我這樣做時-調試有效。
有沒有辦法將我的VS2015設置為始終以提升的特權運行該項目?
嘗試以管理員身份運行Visual Studio。 在遇到特權不足的情況下,以管理員身份運行Visual Studio已解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.