如何在没有管理员访问权限 c# 的情况下以编程方式卸载 UWP 应用程序



我已将 UWP 应用程序旁加载到客户端计算机上。

我现在想卸载该程序,但没有管理员访问权限。

我找到了Remove-AppxPackage,但这使用powershell,因此需要一个需要管理员访问权限executionpolicy

对于我的 WPF 应用程序,我只会删除包含该应用程序的目录,但对于 UWP 应用程序,我什至不确定要删除什么。

本质上,我想以编程方式单击"添加和删除程序"中的卸载按钮

我确实看过这个链接 如何使用代码以编程方式卸载应用程序:

public static string GetUninstallCommandFor(string productDisplayName)
{
RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,RegistryView.Registry64);
string productsRoot = @"SOFTWAREMicrosoftWindowsCurrentVersionInstallerUserDataS-1-5-18Products";
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 "";
}

但这没有找到我的应用程序 - 即使它在"应用程序和功能"设置页面中

好的,我按照Nico Zhu的建议使用Powershell的解决方案。我创建了一个这样的方法:

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

并像这样使用它:

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

令人惊讶的是,此过程不需要管理员权限。

我必须说,从微软开发人员的角度来看,用户体验。为了管理我的 UWP 应用的分发,这是 UWP 与 WPF 的另一个大拇指

最新更新