vsto项目点击but后调用同更目录下的以管理员身份运行demo.exe

vsto项目点击but后调用同更目录下的以管理员身份运行demo.exe
  1. Verb = "runas":触发Windows UAC弹窗,请求管理员权限
  2. 路径处理:使用Assembly.GetExecutingAssembly().Location获取VSTO插件部署路径,避免硬编码。
  3. 异常处理:捕获Win32Exception以处理用户拒绝权限的情况。
复制代码
private void button1_Click(object sender, RibbonControlEventArgs e)
{
    // 获取当前程序所在目录
    string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string exePath = Path.Combine(currentDir, "demo.exe");

    var startInfo = new ProcessStartInfo
    {
        FileName = exePath,
        UseShellExecute = true,
        Verb = "runas",  // 关键:触发UAC提权
        WorkingDirectory = currentDir
    };

    try
    {
        Process.Start(startInfo);
    }
    catch (Win32Exception ex)
    {
        // 用户拒绝UAC弹窗时捕获异常
        MessageBox.Show("权限请求被拒绝:" + ex.Message);
    }
}
 
posted @ 2025-08-09 16:09  1010阿龙  阅读(9)  评论(0)    收藏  举报