由于SQL Server安装是可选的,因此我没有将其作为安装程序的先决条件。相反,我只是将
sqlexpr32.exe
在“我的安装”介质中安装,并使用适当的命令行参数以编程方式启动它,以便执行自动安装。(注意:我使用的是/qb命令行标志,因此安装不是静默的,
).万一有人想知道,我就跟着
this Microsoft article
// All this runs on a background thread so the user
// can cancel my app's setup at any time
// Launch the installer
Process setupProcess = new Process();
setupProcess.StartInfo.FileName = "sqlexpr32.exe";
setupProcess.StartInfo.Arguments = " a bunch of command line args here";
setupProcess.StartInfo.UseShellExecute = false; // to avoid a shell window
setupProcess.Start();
// At this point the SQL Server installer is running
// Monitor the process on 2-second intervals:
while (!setupProcess.WaitForExit(2000))
{
if(WasCancelled) // flag that is set when the user cancels my app's setup
{
// This following line is my problem. Sending CloseMainWindow does not
// seem to work. The SQL Server installer just keeps running.
setupProcess.CloseMainWindow();
setupProcess.WaitForExit();
break;
}
}
// After this point I build a results report for the user.
// My app's installer does not yet quit even if it was canceled.
所以我的问题是:
这一行似乎没有任何作用:
setupProcess.CloseMainWindow();
这也不起作用:
setupProcess.Close(); // This closes my handle. Not the target process.
很明显,我不想仅仅终止进程,因为我可能会让用户的机器处于一种不太理想的状态,最好的情况是有大量垃圾文件,或者更糟的是,安装已损坏。
有什么想法吗?发送按键还是模拟用户点击?还是希望能少点麻烦?
编辑:
我想我已经找到了原因
CloseMainWindow
不起作用:
-
我开始的过程(
sqlexpr32.exe
真实的
setup.exe
作为子进程。所以这个问题变得更加困难=(