【问题标题】:.NET Core 2.0 - Can't access ProcessStartInfo.Verb [closed].NET Core 2.0 - 无法访问 ProcessStartInfo.Verb [关闭]
【发布时间】:2018-01-04 04:49:42
【问题描述】:

我一直在使用 .NET Core 2.0,但在尝试使用 UAC 提示启动应用时遇到了问题。

为此,我通常会将 ProcessStartInfo 的 Verb 属性设置为“runas”,但该属性显然未定义。

我发现了这个问题: Launch process that needs to trigger UAC in netcore

在评论中,我发现 .NET Core 2.0 旨在包含动词和动词,并且文档也包含它们。

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.verbs?view=netcore-2.0

我的 Visual Studio 中的某些设置是否不正确?还是这些实际上仍然不受支持?

值得注意的是,我的“目标框架”确实设置为“.NET Core 2.0”。

根据要求,这是我的方法:

public static bool CheckOrRequestAdmin(string command)
{
    if (IsRunAsAdmin())
    {
        return true;
    }

    ProcessStartInfo proc = new ProcessStartInfo();
    proc.UseShellExecute = true;
    proc.WorkingDirectory = Assembly.GetEntryAssembly().Location;
    proc.FileName = Assembly.GetEntryAssembly().Location;
    proc.Verb = "runas";
    proc.Arguments = command;

    try
    {
        Process.Start(proc, );
    }
    catch
    {
        // The user refused the elevation.
        // Do nothing and return directly ...
        return false;
    }

    Environment.Exit(0);
    return true;
}

【问题讨论】:

  • 请提供代码,该代码不起作用。您在哪些平台上尝试过?您尝试启动的文件名是什么?
  • 代码现在在原始帖子中。我在Win10上,在VS2017中运行。由于这是编译时间问题,而不是运行时问题,因此文件名无关紧要。在这种情况下,它只是调用该方法的可执行文件的名称。

标签: c# .net-core processstartinfo


【解决方案1】:

经典!一问才发现。

虽然我设置了 Target 框架,但我需要更新 .NET Core 应用程序的 Nuget 包,它仍然使用旧版本。

更新后,完全按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-11
    • 2021-09-12
    • 1970-01-01
    • 2020-08-05
    • 2019-05-09
    • 2020-09-23
    • 2018-01-07
    • 1970-01-01
    相关资源
    最近更新 更多