power shell 删除应用
2024-09-08 19:29:51
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;
}
}
很难看懂 不推荐看不懂的人用
最新文章
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
- Java基础--serialVersionUID
- YII2项目常用技能知识总结
- python json学习之路2-认识python种的json模块
- SSH Secure Shell Client的windows客户端样式设置
- Windows2008 R2下,DCOM配置里的属性灰色不可用的解决方法
- 161014、Comet4J介绍及使用(即时推送)
- Oracle GoldenGate 11.2 OGG-01168(转)
- NSS_01 开始
- #include<;unistd.h>;存在linux中,含有系统服务的函数
- <;hdu - 1232>; 畅通工程 并查集问题 (注意中的细节)
- FZU 1502 Letter Deletion(DP)
- python制作爬虫爬取京东商品评论教程
- mongodb与mysql命令详细对比
- 软工个人博客作业Week 1
- python+django+uwsgi 搭建环境
- 《centos系列》git的安装
- Spring框架的事务管理之基于AspectJ的注解方式(重点掌握,最简单的方式)
- 我学cocos2d-x (三) Node:一切可视化对象的祖先
- day25<;多线程+&;设计模式&;GUI>;