Search code examples
c#.net-coreprocessstartinfo

.NET Core 2.0 - Can't access ProcessStartInfo.Verb


I've been working with .NET Core 2.0, and I've come across an issue while trying to launch an app with the UAC prompt.

To do this, I would generally set the Verb property of ProcessStartInfo to 'runas', however the property is apparently undefined.

I found this question: Launch process that needs to trigger UAC in netcore

Within a comment, I found out that .NET Core 2.0 was meant to contain both Verb and Verbs, and the documentation also includes them.

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

Is something in my Visual Studio set up incorrectly? Or are these actually still unsupported?

Worth noting, my 'Target framework' is indeed set to '.NET Core 2.0'.

As requested, here is my method:

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;
}

Solution

  • Classic! Found it just after asking.

    Although I had Target framework set, I needed to update the Nuget package for .NET Core App, which was still sitting on an old version.

    After updating that, worked exactly as expected.