繁体   English   中英

始终以提升的权限运行VS2015调试

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM