从vs2012起,微软已经不支持setup project了。以此纪念一下setup project。
在新建Setup Project
增加安装内容,通常是直接Oupput一个项目,或者直接添加exe,dll
File System中,可以选择程序安装的目录。
注意一点,Setup Project本身不提供卸载的功能。需要自己添加。
可以在Application Folder添加Uninstall.bat文件,内容是:
C:Windowssystem32MsiExec.exe /I{ProduceCode}
ProduceCode需替换成Setup Project属性中的ProduceCode
还可以在User' Programs Menu中添加指向Uninstall.bat的快捷链接
这里提到可以调用系统自带的 msiexec.ext 进行卸载。但是经网友测试得出如下结论:
Awesome, works very well for me on WinXP/Vista & Win7. Worth noting that according to the article linked to, if you use a version of msiexec.exe HIGHER than the target platform there are issues. This is not an issue for me as I'm still intentionally using XP for building - but obviously if you want an installer built on Vista to run on XP that will be an issue.
也有网友提出如下解决方案
Regarding the problem that arises when building the Installer under Vista/7, and the installer tries to overwrite msiexec on older, XP machines; this can be corrected by creating an empty .txt file, and renaming it to "msiexec.exe." Include that file in your installer, rather than the actual System32 version. Since the empty, fake file has no version information, it will not try to overwrite the XP msiexec
但我试过,是行不通的。>_<
所以,还是乖乖用回Uninstall.bat实现卸载。
增加User Interface
在这里可以增加自定义的安装界面,但是只能用它定义好的样式,不能内嵌页面
增加Custom Action
这里可以添加自定义的,针对安装期间生命周期的事件。我这里是在dll中定义了一个InstallHelper的类,在安装的过程中写一个xml文件
[RunInstaller( true )]
public class InstallHelper : Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
base .Install(stateSaver);
try
{
string brandType = string .IsNullOrEmpty(Context.Parameters[ "BrandType" ]) ? "HuaShi" : Context.Parameters[ "BrandType" ];
string assemblypath = Context.Parameters[ "assemblypath" ];
string pathName = Path.Combine(Path.GetDirectoryName(assemblypath), brandType + ".xml" );
if (File.Exists(pathName))
{
File.Delete(pathName);
}
using (Stream st = File.Open(pathName, FileMode.OpenOrCreate))
{
var writer = XmlWriter.Create(st);
writer.WriteStartDocument();
writer.WriteStartElement( "Info" );
writer.WriteElementString( "Seed" , System.Guid.NewGuid().ToString());
writer.WriteElementString( "Brand" , brandType);
writer.WriteEndElement();
writer.Close();
}
}
catch (FormatException e)
{
string s = e.Message;
}
}
}
其中 Context.Parameters[ "BrandType" ] 是在 CustomActionData中定义的。
BrandType是需要传递的参数名
[BRANDTYPE]是在User Interface自定义界面中控件的ID
参考:
- http://www.codeproject.com/Articles/12780/A-Setup-and-Deployment-project-that-passes-paramet
- http://stackoverflow.com/questions/7999822/net-setup-project-how-to-pass-multiple-customactiondata-fields
- http://stackoverflow.com/questions/1356160/in-a-visual-studio-setup-project-how-do-i-generate-an-uninstall-script
- http://forums.codeguru.com/showthread.php?301618-Create-an-Uninstall-Shortcut-in-the-StartMenu&p=978211#post978211