power shell 删除应用

2021/10/1 7:42:59

本文主要是介绍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;
            }
        }

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



这篇关于power shell 删除应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程