C# 使用C语言以编程方式卸载软件#
我想用我的代码卸载一个软件,我已经尝试了wmic方法来执行卸载,但它无法在系统中找到我的软件。是否可以在不使用msi文件或任何安装文件的情况下卸载。我找到了这个代码,但它不起作用---C# 使用C语言以编程方式卸载软件#,c#,uninstallation,C#,Uninstallation,我想用我的代码卸载一个软件,我已经尝试了wmic方法来执行卸载,但它无法在系统中找到我的软件。是否可以在不使用msi文件或任何安装文件的情况下卸载。我找到了这个代码,但它不起作用--- 最可靠的方法是以编程方式执行以下shell命令: msiexec.exe /x {PRODUCT-GUID} 如果您制作了原始MSI,您将可以访问产品GUID,这就是您所需要的。不需要实际的MSI文件,因为Windows正是为了这个目的而保存了它的副本 仅供参考: Windows ® Installer. V
最可靠的方法是以编程方式执行以下shell命令:
msiexec.exe /x {PRODUCT-GUID}
如果您制作了原始MSI,您将可以访问产品GUID,这就是您所需要的。不需要实际的MSI文件,因为Windows正是为了这个目的而保存了它的副本
仅供参考:
Windows ® Installer. V 5.0.14393.0
msiexec /Option <Required Parameter> [Optional Parameter]
Install Options
</package | /i> <Product.msi>
Installs or configures a product
/a <Product.msi>
Administrative install - Installs a product on the network
/j<u|m> <Product.msi> [/t <Transform List>] [/g <Language ID>]
Advertises a product - m to all users, u to current user
</uninstall | /x> <Product.msi | ProductCode>
Uninstalls the product
Display Options
/quiet
Quiet mode, no user interaction
/passive
Unattended mode - progress bar only
/q[n|b|r|f]
Sets user interface level
n - No UI
b - Basic UI
r - Reduced UI
f - Full UI (default)
/help
Help information
Restart Options
/norestart
Do not restart after the installation is complete
/promptrestart
Prompts the user for restart if necessary
/forcerestart
Always restart the computer after installation
Logging Options
/l[i|w|e|a|r|u|c|m|o|p|v|x|+|!|*] <LogFile>
i - Status messages
w - Nonfatal warnings
e - All error messages
a - Start up of actions
r - Action-specific records
u - User requests
c - Initial UI parameters
m - Out-of-memory or fatal exit information
o - Out-of-disk-space messages
p - Terminal properties
v - Verbose output
x - Extra debugging information
+ - Append to existing log file
! - Flush each line to the log
* - Log all information, except for v and x options
/log <LogFile>
Equivalent of /l* <LogFile>
Update Options
/update <Update1.msp>[;Update2.msp]
Applies update(s)
/uninstall <PatchCodeGuid>[;Update2.msp] /package <Product.msi | ProductCode>
Remove update(s) for a product
Repair Options
/f[p|e|c|m|s|o|d|a|u|v] <Product.msi | ProductCode>
Repairs a product
p - only if file is missing
o - if file is missing or an older version is installed (default)
e - if file is missing or an equal or older version is installed
d - if file is missing or a different version is installed
c - if file is missing or checksum does not match the calculated value
a - forces all files to be reinstalled
u - all required user-specific registry entries (default)
m - all required computer-specific registry entries (default)
s - all existing shortcuts (default)
v - runs from source and recaches local package
Setting Public Properties
[PROPERTY=PropertyValue]
Windows®安装程序。V 5.0.14393.0
msiexec/Option[可选参数]
安装选项
安装或配置产品
/a
管理安装-在网络上安装产品
/j[/t][g]
向所有用户宣传产品-m,向当前用户宣传产品-u
卸载产品
显示选项
/安静的
安静模式,无用户交互
/被动的
无人值守模式-仅进度条
/q[n | b | r | f]
设置用户界面级别
n-无用户界面
b-基本用户界面
r-Reduced用户界面
f-完整用户界面(默认)
/帮助
帮助信息
重新启动选项
/诺雷斯塔特
安装完成后不要重新启动
/迅速重启
如有必要,提示用户重新启动
/强制重启
安装后务必重新启动计算机
记录选项
/l[i | w | e | a | r | u | c | m | o | p | v | x |+|!|*]
i-状态消息
w-非致命警告
e-所有错误消息
a-行动启动
r-行动特定记录
用户请求
c-初始UI参数
m-内存不足或致命的退出信息
o-磁盘空间不足消息
p-终端特性
v-详细输出
x-额外调试信息
+-附加到现有日志文件
! - 刷新日志中的每一行
*-记录所有信息,v和x选项除外
/日志
相当于1/l*
更新选项
/update[;Update2.msp]
应用更新
/卸载[;Update2.msp]/package
删除产品的更新
修复选项
/f[p | e | c | m | s | o | d | a | u | v]
修理产品
p-仅当文件丢失时
o-如果文件丢失或安装了旧版本(默认)
e-如果文件丢失或安装了相同或旧版本
d-如果文件丢失或安装了其他版本
c-如果文件丢失或校验和与计算值不匹配
a-强制重新安装所有文件
u-所有必需的特定于用户的注册表项(默认)
m-所有必需的计算机特定注册表项(默认)
s-所有现有快捷方式(默认)
v-从源代码运行并重新缓存本地包
设置公共属性
[属性=属性值]
试试这个
我们使用以下格式获取ManagementObject属性:
列出已安装应用程序的完整代码块:
using System.Management
private List<string> ListPrograms()
{
List<string> programs = new List<string>();
try
{
ManagementObjectSearcher mos =
new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject mo in mos.Get())
{
try
{
//more properties:
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa394378(v=vs.85).aspx
programs.Add(mo["Name"].ToString());
}
catch (Exception ex)
{
//this program may not have a name property
}
}
return programs;
}
catch (Exception ex)
{
return programs;
}
}
这是一个老问题,回答可能太晚了,但这是我的代码
public static void UninstallApplication(string uninstallString)
{
if (string.IsNullOrEmpty(uninstallString))
{
throw new ArgumentNullException(nameof(uninstallString));
}
ProcessStartInfo startInfo = new ProcessStartInfo();
int indexofexe = uninstallString.IndexOf(".exe");
//Check for executable existence
if (indexofexe > 0)
{
uninstallString = uninstallString.Replace(@"""", string.Empty);
//Get exe path
string uninstallerPath = uninstallString.Substring(0, indexofexe + 4);
startInfo.FileName = uninstallerPath;
//Check for arguments
if (uninstallerPath.Length != uninstallString.Length)
{
string args = uninstallString.Substring(uninstallerPath.Length);
if (!string.IsNullOrEmpty(args))
{
/*If not set to false You will get InvalidOperationException :
*The Process object must have the UseShellExecute property set to false in order to use environment variables.*/
startInfo.UseShellExecute = false;
startInfo.Arguments = args;
}
}
}
//Not tested
else
{
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + uninstallString;
}
//Start the process
Process.Start(startInfo).WaitForExit();
}
这在我的大多数应用程序上都有效“但它不起作用”是一个无用的问题陈述。因此,你能给我建议一种方法吗?…这看起来像是一个复制品。唯一可靠的方法是使用:“msiexec.exe/x{your product code guid}”,我建议您描述一下“它不工作”的意思。我使用installshield创建了一个软件包,并以静默方式安装它,现在我想以静默方式卸载它,我的问题是,在不使用msi文件或安装文件的情况下,是否可以添加
/quiet
。您还可以通过查看msiexec/?
的结果来探索许多其他选项。这是如何“工作”的?我有if(mo[“Name”].ToString().Contains(“Skype”))对象hr=mo.InvokeMethod(“卸载”,null)
从我的机器上卸载Skype,但即使在代码运行后,Skype仍然存在。与@Ron16有相同的问题。有人找到解决办法了吗?Ron16在哪里可以找到解决方案?
private bool UninstallProgram(string ProgramName)
{
try
{
ManagementObjectSearcher mos = new ManagementObjectSearcher(
"SELECT * FROM Win32_Product WHERE Name = '" + ProgramName + "'");
foreach (ManagementObject mo in mos.Get())
{
try
{
if (mo["Name"].ToString() == ProgramName)
{
object hr = mo.InvokeMethod("Uninstall", null);
return (bool)hr;
}
}
catch (Exception ex)
{
//this program may not have a name property, so an exception will be thrown
}
}
//was not found...
return false;
}
catch (Exception ex)
{
return false;
}
}
public static void UninstallApplication(string uninstallString)
{
if (string.IsNullOrEmpty(uninstallString))
{
throw new ArgumentNullException(nameof(uninstallString));
}
ProcessStartInfo startInfo = new ProcessStartInfo();
int indexofexe = uninstallString.IndexOf(".exe");
//Check for executable existence
if (indexofexe > 0)
{
uninstallString = uninstallString.Replace(@"""", string.Empty);
//Get exe path
string uninstallerPath = uninstallString.Substring(0, indexofexe + 4);
startInfo.FileName = uninstallerPath;
//Check for arguments
if (uninstallerPath.Length != uninstallString.Length)
{
string args = uninstallString.Substring(uninstallerPath.Length);
if (!string.IsNullOrEmpty(args))
{
/*If not set to false You will get InvalidOperationException :
*The Process object must have the UseShellExecute property set to false in order to use environment variables.*/
startInfo.UseShellExecute = false;
startInfo.Arguments = args;
}
}
}
//Not tested
else
{
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + uninstallString;
}
//Start the process
Process.Start(startInfo).WaitForExit();
}