power shell 删除应用

        public static UwpAppInfo SearchUwpAppByName(string appName)
        {
            UwpAppInfo app = null;
            try
            {
                string resultOutput;
                Process proc = new Process();
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.Arguments = string.Format("/C powershell Get-AppxPackage -Name \"{0}\"", appName);
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.WaitForExit();
                resultOutput = proc.StandardOutput.ReadToEnd();
                appName = appName.Replace("*", "");
                if (resultOutput.Contains(appName))
                {
                    app = new UwpAppInfo();
                    string[] appInfo = resultOutput.Replace("\r\n", ":").Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    app.Name = GetAppVauleByItemName(appInfo, "Name");
                    app.Arch = GetAppVauleByItemName(appInfo, "Architecture");
                    app.Version = GetAppVauleByItemName(appInfo, "Version");
                    app.PackageFullName = GetAppVauleByItemName(appInfo, "PackageFullName");
                    app.PackageFamilyName = GetAppVauleByItemName(appInfo, "PackageFamilyName");
                    app.InstallLocation = GetAppVauleByItemName(appInfo, "InstallLocation");
                    app.PublisherId = GetAppVauleByItemName(appInfo, "PublisherId");
                }
                proc.Close();
                return app;
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog("SearchUwpAppByName UWP excepiton:" + ex.Message, LogType.Error);
                return null;
            }
        }

        public static void Uninstallapp()
        {
            string LenovoUtilityAppIdName = "E0469640.LenovoUtility_5grkq8ppsgwt4(app名吧大概)";
            UwpAppInfo comp = SearchUwpAppByName(LenovoUtilityAppIdName);
            if (comp != null)
            {
                UninstallUwpApp(comp.PackageFullName);
            }
        }

        public static void RunPowershellCmdlet(string cmdlet)
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipe = runspace.CreatePipeline();
            pipe.Commands.AddScript(cmdlet);
            pipe.Invoke();
            runspace.Close();
        }

        public static bool UninstallUwpApp(string appFullName)
        {
            string packageCachePath = @"C:\ProgramData\Packages\E046963F.LenovoCompanion_k1h2ywk1493x8";
            int timeOut = 0;
            try
            {
                RunPowershellCmdlet("Remove-AppxPackage " + appFullName.Trim());
                /*
                Process proc = new Process();
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.Arguments = string.Format("/C powershell Remove-AppxPackage \"{0}\"", appFullName);
                proc.StartInfo.UseShellExecute = true;
                proc.Start();
                proc.WaitForExit();
                proc.Close();
                */
                while (Directory.Exists(packageCachePath) && timeOut < 10)
                {
                    Directory.Delete(packageCachePath, true);
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    timeOut++;
                }(好像是用来验证的)

                return true;
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteLog("Uninstall UWP excepiton:" + ex.Message, LogType.Error);
                return false;
            }
        }

很难看懂 不推荐看不懂的人用

上一篇:2021年9月27日


下一篇:一次关于k8s kubectl top 和 contained ps 不一致的问题探究