C# 始终使用提升的权限运行VS2015调试

C# 始终使用提升的权限运行VS2015调试,c#,winforms,permissions,visual-studio-2015,C#,Winforms,Permissions,Visual Studio 2015,我在Program.cs中有一个winform应用程序,其中包含以下代码,以确保我的应用程序始终以管理权限运行: static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { // Init visual sty

我在
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);
    }
}
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
//初始化视觉样式
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//检查应用程序是否以管理权限启动
WindowsPrincipal=新的WindowsPrincipal(WindowsIdentity.GetCurrent());
bool administrativeMode=principal.IsInRole(WindowsBuiltInRole.Administrator);
//检查应用程序是否已在运行
布尔mutexCheck;
var mutex=new mutex(true,Assembly.getExecutionGassembly().GetType().GUID.ToString(),out mutexCheck);
如果(!mutexCheck)
{
//错误
Utils.bathror(“程序已在运行”);
返回;
}
//如果应用程序在没有管理权限的情况下运行
如果(!管理模式)
{
//请求行政特权
ProcessStartInfo startInfo=新的ProcessStartInfo();
startInfo.Verb=“runas”;
startInfo.FileName=Application.ExecutablePath;
尝试
{
//以管理员身份运行应用程序
进程启动(startInfo);
}
捕获(例外情况除外)
{
//错误
Utils.bathror(“程序启动失败-”+ex.Message);
返回;
}
}
返回;
//启动应用程序
运行(新的MyApp());
GC.KeepAlive(互斥);
}
}
每次我在Visual Studio 2015中按
F5
以调试模式运行应用程序时,应用程序似乎正在运行-调试环境未启动

因此,我要做的是转到
Debug->Attach to Process…
并选择
MyApp.exe
,然后我会收到以下窗口的提示:

然后,当我单击“在不同凭据下重新启动”时,VS2015将重新启动。然后,我必须关闭已经运行的MyApp实例,然后按
F5
以提升权限重新启动;当我这样做的时候,调试工作就开始了


有没有办法将我的VS2015设置为始终以提升的权限运行此项目

尝试以管理员身份运行Visual Studio。当我以管理员身份运行Visual Studio时遇到权限不足的情况,我已经解决了这个问题。

我通过遵循此处为
Windows 8和8.1
概述的步骤实现了这一点(我使用的是Windows 10,这很有效)。现在,我可以启动项目解决方案文件,它会自动启动具有管理员权限的VS2015:)