﻿# 如何將自訂參數傳入 Installer 類別的 Install 方法

雖然 Windows Setup Project 安裝專案很好用，也可以自訂使用者介面，但限制頗多，只要自訂的使用者介面多一些條件就沒辦法客製化了，所以我們打算改用 [NSIS](http://nsis.sourceforge.net/) 進行安裝過程的介面自訂，最後再將透過 UI 介面取得的參數傳入 [Installer 類別](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.installer.aspx)中，並利用 C# 撰寫的程式碼幫我們完成 Post-Install 的動作執行，例如安裝 IIS 站台、建立資料庫、建立使用者或群組、... 等。

首先我是先看到 Installer 類別中的 [Install 方法](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.installer.install.aspx)可以傳入一個 IDictionary 的資料：

![Installer.Install 方法](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/e269f67b0960fd11b3b8-image_3.png)

最近研究了 [Msiexec.exe](http://technet.microsoft.com/zh-tw/library/cc759262.aspx) 與 [InstallUtil.exe](http://msdn.microsoft.com/zh-tw/library/50614e95.aspx) 工具，試圖想在 Post-Install 的過程可以傳入準備好的自訂的參數到 [Installer 類別](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.installer.aspx)，雖然 Installer 類別中的 [Install 方法](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.installer.install.aspx)可以傳入一個 IDictionary 的資料，不過我就是從 [Msiexec.exe](http://technet.microsoft.com/zh-tw/library/cc759262.aspx) 與 [InstallUtil.exe](http://msdn.microsoft.com/zh-tw/library/50614e95.aspx) 工具中找不到有任何方法可以傳入自訂的參數集合。

最後才發現原來有個 [AssemblyInstaller 類別](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.assemblyinstaller.aspx)可以用，透過這個類別來包裹 [InstallUtil.exe](http://msdn.microsoft.com/zh-tw/library/50614e95.aspx) 工具進行安裝動作，就可以輕易的傳入任意參數到安裝過程的各種 method 中。

但才開心沒多久，這條路又被堵住了，透過 [Reflector](http://www.red-gate.com/products/reflector/) 查看 Install 方法才發現，原來傳入的 savedState 參數在傳入的時候就會立即被一行 new Hashtable(); 給複寫掉，所以傳入任何參數也都是沒用的。

![AssemblyInstaller.Install method: 傳入的 savedState 被蓋掉了](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/74208be86ee7ab3b3721-image_6.png) 

不止 Install 方法不能傳，Commit, Rollback, Uninstall 方法也都會被複寫掉，所以這條路是不通的。

而且由此可知，在 Installer 類別內的 savedState 僅用於安裝過程中紀錄各種安裝狀態用，並無法用來做其他用途，例如當成傳入參數的管道！

那好吧，該試的方法 "應該" 都試了吧，使用 Windows / .NET 內建的安裝工具我覺得已經**無解**了，如果有人有方法，煩請告知！

目前我只能**向外**發展，找尋 [NSIS](http://nsis.sourceforge.net/) 的 [Plug-ins](http://nsis.sourceforge.net/Category:Plugins) 或 [InnoSetup](http://www.innosetup.com/) 等不同的安裝部署解決方案，讓我能夠自訂更細的安裝過程。

我個人是還在期待 [WiX](http://wix.sourceforge.net/) 的降臨，因為 WiX 3.0 的 Beta 版即將結束，馬上就要 [RTM](http://en.wikipedia.org/wiki/Software_release_life_cycle#RTM) 了，且未來會整合進 [Visual Studio 2010](https://visualstudio.microsoft.com/vs/older-downloads/)，成為新的預設安裝專案類型，到時應該會有更完善的開發介面，而不用再辛苦的撰寫 XML 定義檔了吧。

**相關連結**

-   [Installer 類別](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.installer.aspx)
-   [AssemblyInstaller 類別](http://msdn.microsoft.com/zh-tw/library/system.configuration.install.assemblyinstaller.aspx)
-   [Msiexec (命令列選項)](http://technet.microsoft.com/zh-tw/library/cc759262.aspx)
-   [安裝程式工具 (Installutil.exe)](http://msdn.microsoft.com/zh-tw/library/50614e95.aspx)
-   [How to Use Property Reference Command-Line Parameters with Msiexec.exe](https://web.archive.org/web/20130619235938/http://support.microsoft.com/kb/230781/en-us)
-   [Windows Installer XML (WiX) toolset](http://wix.sourceforge.net/)
-   [製作安裝程式--NSIS](http://vfp.sunyear.com.tw/viewtopic.php?t=1294&start=0&postdays=0&postorder=asc&highlight=&sid=08c0297b703f9c0ec7d5104e15dd091e)
-   [NSIS Discussion](http://forums.winamp.com/forumdisplay.php?forumid=65)
-   [NSIS 用戶手冊](http://www.omega.idv.tw/nsis/Contents.html) (中文版)
