JKennedy
JKennedy

Reputation: 18817

How to uninstall a UWP application programmatically without admin access c#

I have sideloaded a UWP application onto my clients machine.

I would now like to uninstall the program, but without admin access.

I have found Remove-AppxPackage but this uses powershell and so would need an executionpolicy set which would require admin access

For my WPF applications I would just delete the directory containing the application but with a UWP app I'm not even sure what to delete.

Essentially I would like to programatically click the uninstall button on from the Add and remove programs

I did look at this link How to uninstall application programmatically with the code:

    public static string GetUninstallCommandFor(string productDisplayName)
    {
        RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,RegistryView.Registry64);
        string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products";
        RegistryKey products = localMachine.OpenSubKey(productsRoot);
        string[] productFolders = products.GetSubKeyNames();

        foreach (string p in productFolders)
        {
            RegistryKey installProperties = products.OpenSubKey(p + @"\InstallProperties");
            if (installProperties != null)
            {
                string displayName = (string)installProperties.GetValue("DisplayName");
                Debug.WriteLine(displayName);
                if ((displayName != null) && (displayName.Contains(productDisplayName)))
                {
                    string uninstallCommand = (string)installProperties.GetValue("UninstallString");

                    return uninstallCommand;
                }
            }
        }

        return "";
    }

But this didn't find my application - eventhough it is in the "Apps and features" settings page

Upvotes: 0

Views: 1309

Answers (1)

JKennedy
JKennedy

Reputation: 18817

Ok my solution as advised by Nico Zhu was to use powershell. I created a method like so:

    private static void LaunchProcess(string uri, string args)
    {
        var psi = new ProcessStartInfo();
        psi.UseShellExecute = true;
        psi.CreateNoWindow = false;
        psi.Arguments = args;
        psi.WindowStyle = ProcessWindowStyle.Hidden;
        psi.FileName = uri;
        var proc = Process.Start(psi);

        proc.WaitForExit();
        var exitcode =  proc.ExitCode;
    }

and used it like so:

LaunchProcess("powershell.exe", "get-appxpackage *AppPackageNameThatOnlyMatchesYourAppPackage* | remove-appxpackage");

This process surprisingly didn't require admin rights.

I must say though from a microsoft developer point of view UX. For managing distribution of my UWP apps, this is another thumbs down for UWP vs WPF

Upvotes: 1

Related Questions