How to make my dotnet tool to auto self update

495 views Asked by At

I cannot see how to configure my dotnet tool (c# .net 6) to auto update

If I execute dotnet tool update <tool> --global --no-cache in process then it complains about the file already existing.

It seems that we need something external to do the update and I was expecting dotnet.exe would support this better. Are there any best practices here?

1

There are 1 answers

0
Sam Mackrill On BEST ANSWER

This is what I have come-up with:

private const int WaitForExit = 20;
/// <returns>Exit Code</returns>
public void ForceUpdate()
{
    Console.WriteLine($"{ApplicationName} update requested");
    var pid = Process.GetCurrentProcess().Id;
    var command = $@"Wait-Process -Id {pid} -Timeout {WaitForExit} -ErrorAction SilentlyContinue; dotnet tool update --global {ApplicationName}";
    Process.Start(new ProcessStartInfo
    {
        FileName = "powershell.exe",
        Arguments = $"-NoProfile -NoLogo -NonInteractive -ExecutionPolicy unrestricted -command {command}",
        UseShellExecute = false
    });
}