使用 msiexec 和 c# 安装 msi [英] Install msi with msiexec and c#

查看:63
本文介绍了使用 msiexec 和 c# 安装 msi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以静默模式在 c# 应用程序中安装 .msi 的最佳方法是什么.我想使用 msiexec 安装 .msi 文件,但我不知道如何执行此操作.问题是将 msiexec 与/qn 一起使用,您必须在以管理员身份"启动的 cmd.exe 进程中运行它,我不知道如何使用 ac# 应用程序完成类似的操作(有关更多详细信息,请参阅 wpf 项目在 VS 2010 中).我来的最好的是:

What is the best way to install a .msi in a c# application in silent mode. I want to install a .msi file using msiexec, but I don't know how to do this. The problem is for using msiexec with /qn, you have to run it in a cmd.exe process start "as a administrator" and I don't know how to accomplish something similar with a c# application (for more details, a wpf project in VS 2010). The best I come to is :

Process p = new Process();
p.StartInfo.FileName = "runas.exe";
p.StartInfo.Arguments = "/user:Administrator cmd";
p.Start();

会弹出一个窗口并要求输入密码,但我发现这种行为非常丑陋.我的意思是,我不想向用户询问他的密码.有没有办法启动我的项目,将其设置为一种以管理员身份运行"(UAC 会弹出,但没关系)然后只运行 msiexec 命令?或者有没有其他方法可以静默安装 .msi 文件?

A windows will pop and ask for a password, but I found this behavior pretty ugly. I mean, I don't want to ask the user for his password. Is there anyway way to start my project, set it as a kind of "run as administrator" (the UAC will pop-up, but that's ok) and just run the msiexec command ? Or is there any other way to make a silent install of a .msi file ?

谢谢

推荐答案

尽管有人在没有评论的情况下投票否决,但 IMO 是一个非常彻底的答案:也许有人误解了,因为答案很长.短文推荐:选择选项 2).

Despite somebody voted down without comment, this is a very thorough answer IMO: Maybe somebody misunderstood, because the answer is quite long. Recommendation for short reading: Take option 2) .

首先,一般来说,尽量避免使用 RunAs.使用 RunAs,您正在创建一种并非所有场景和应用程序/流程都支持的混合用户情况.对于比 XP 更新的操作系统,它并不是真正必要的.需要 UAC 的管理员权限(例如 .exe 中的正确清单)至少是 MS 的建议!(只是一个安全说明:最安全的方法仍然是将管理员帐户完全分开,这样通常就不需要 RunAs 和 UAC.)

First, generally, try to avoid RunAs as often you can. With RunAs you are creating a mixed user situation which is not supported by all scenarios and applicatons/processes. With operating systems newer than XP it is not really necessary. Requiring admin rights with UAC (e.g. the correct manifest in the .exe) is at least MS recommendation ! (Just a security remark: Most secure way is still having the admin account completely separated so that neither RunAs nor UAC are normally needed.)

1) 确保 MSI 安装的管理员权限的最简单(但不是我推荐的)方法是什么都不做,让 UAC 工作.

1) The most easy (but not my recommended) method to assure admin rights for a MSI install is doing nothing and let UAC work.

如果 MSI 需要管理员权限,UAC 通常会在一段时间后自行出现(并非安装过程的每个部分都需要管理员权限,因此需要一些时间.

If the MSI needs admin rights, UAC will normally come up itself after some time (not every part of the install process needs admin rights, so it will take some time.

为了确保 UAC 请求出现在子进程中,有点像 UAC 继承,您必须在基线中使用 ShellExecute.这是 Windows 的东西,与 MSI 或 C# 无关.

To ensure, that UAC requests come up in child processes, kinda UAC-inheritance, you have to use ShellExecute in the baseline. This is a Windows thing, has nothing to do with MSI or C#.

在 C# 中,你可以这样做:

In C# you can do this like this:

ProcessStartInfo startInfo = new ProcessStartInfo(exeFile, arguments);
StartInfo.UseShellExecute = true;

这没什么用,如果你想要一个没有安装对话框(例如/qn")的静默安装.

This is of not much use, if you want a silent installation with no installation dialog (e.g. "/qn") at all.

此外,这么晚的 UAC 也有一些小缺点,所以建议使用以下方法,而且非常安全:

Moreover the UAC so late has small disadvantages, so the following is recommended and very much safer:

2) 从一开始就拥有管理员权限,例如在引导程序 .exe 中:如果确定应该开始设置,或者一般来说,您的应用需要对其自己或子进程的管理员权限,最好确保您的应用本身从一开始就拥有管理员权限.
换句话说,UAC 在启动应用程序时出现.那么您的子进程也具有管理员权限,它也适用于 CreateProcess 类型的子进程,而不仅适用于 ShellExecute 类型.

2) Admin rights from the beginning, e.g. in the bootstrapper .exe: If it is sure, that a setup should be started, or in general, your app needs admin rights for it's own or child processes, the best idea ist to assure, that your app itself has already admin rights from the beginning.
In other words, UAC comes up when starting the app. Then your child processes have admin rights too, and it works also for CreateProcess-type child processes and not only for ShellExecute-type.

要实现这一点,只需向您的应用添加一个包含以下行的清单.

To achieve this, just add a manifest to your app containing the following line.

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

怎么做?

鉴于整个 Windows 7 宣传都是基于清单(生命的意义是什么?给应用程序一个清单.")这一事实,Visual Studio 在过去几年的不同版本中并没有真正完成出色的工作支持这一点.每个版本都不同,C#、C++、VB 应用程序也不同.

Given the fact that the whole Windows 7 evangelism was based on manifests ("What is the sense of life? Giving applications a manifest."), Visual Studio in it's different versions of the last years has not really done an excellent job of supporting this. Every version is different, and it is different for C#, C++, VB apps.

对于 VS 2010 和 C#,要走的路是:(对于 VB.Net,它位于解决方案的应用程序"选项卡中的查看 Windows 设置"中,我已在此处阅读.)

For VS 2010 and C# the way to go is: (For VB.Net, it's in 'View Windows Settings' in the solution's Application tab, I've read here.)

在解决方案资源管理器中,右键单击项目并选择添加新项"、新元素"并选择应用程序清单文件".

In Solution Explorer, right click on the project and select "Add New Item", "New element" and choose "Application Manifest File".

通过单击在 VS 中打开它,您将找到现有节点以及描述替代方案的注释行.如果您不了解它们,最好通过谷歌搜索了解有关现代 Windows 的最重要的事情之一.
更改该行,以便配置 level="requireAdministrator",您就完成了.您的应用现在需要提升权限.构建后试用.

Open this in VS by clicking and you will find the existing node of together with comment lines describing the alternatives. If you don't know about them, it is a good point of googling to learn one of the most important things about modern Windows.
Change the line so that level="requireAdministrator" is configured, and you are done. Your app now needs elevated rights. Try out after build.

(uiAccess"属性,您可以忽略,直到您的应用程序控制/自动化其他应用程序的 UI,例如在远程控制或 UI 测试应用程序中.)

(The "uiAccess" attribute, you can ignore, until your app shall control/automate the UI of other apps like in a remote control or UI testing app.)

使用msiexec.exe ..."正常启动 MSI 安装.不要忘记正确位置的引号并捕获错误和重新启动的返回代码.

Start the MSI install normally with "msiexec.exe ...". Don't forget the quotes at the right places and to catch the return code for errors and reboots.

msiexec 的替代方案是使用 MSI API(所谓的外部 UI),但它更复杂.

An alternative to msiexec is using the MSI API (socalled external UI), but it is more complicated.

3) 根本没有管理员权限.不幸的是,这是不可能的或者会破坏标准应用程序的安全策略,这些应用程序应该可以安装在程序目录中的机器范围内.当然还有很多细节要说.

3) No admin rights at all. Unfortunately this is not possible or would break security policies for standard applications which should be installable machine-wide in the programs directory. There is a lot more to say to this in detail of course.

这篇关于使用 msiexec 和 c# 安装 msi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆