我想将其中一些操作从vbscript切换到C#。所有的例子都是以“在VisualStudio中创建一个wix项目…”开始的。是否有任何关于如何向现有的wix项目添加C#代码的示例?其中一个是使用旧版wix命令行应用程序构建的吗?
首先,宣传一下C++自定义操作! :-).
还有:“WiX快速入门”(指向一些好的WiX和MSI资源的指针)。
逐步操作:我来试一试,请尝试以下操作(如果您已经完成了这些初步步骤,可以跳到底部源代码 - 这是真正的逐步操作,非常缓慢,以达到操作 - 您可能可以直接从WiX源代码中获得所需内容):
In WiX Visual Studio solution, right click solution node at top
=> Add
=> New Project...
Expand WiX Toolset node, select v3 (provided that is the version of WiX you use)
Double click "C# Custom Action Project for WiX v3"
Right click "References"
in WiX project (not in C# project) => Add Reference...
Go "Projects"
and add a reference to the C# project
(double click and OK)
Do a test build. Provided there were no errors before there should be none now.
You should see something like "CustomAction1.CA.dll"
in the build Output window. The suffix *.CA.dll
is added to a win32 wrapper dll for the original managed code dll. All of this is handled by WiX itself - or actually the Votive Visual Studio integration for WiX - just know the difference:
"CustomAction1.dll"
- managed code dll."CustomAction1.CA.dll"
- native win32 wrapper dll containing the native one and several other components. Include this version in your MSI
.Add the following snippet:
<Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" />
The above should compile the actual C# dll into the MSI. You can open the MSI in Orca and see in the Binary table.
It is not great, but I like to add a reference to System.Windows.Forms
and use a MessageBox.Show
to show a dialog from within the custom action to ensure it is running as expected. I also add the application debugger launch command for dlls built in debug mode. That way Visual Studio will be automatically invoked (if all works correctly) so the code can be stepped through.
Add the reference to "System.Windows.Forms"
by right clicking the C# project's Reference node and then add "System.Windows.Forms"
. Also add "using System.Windows.Forms;"
to the top of the source file - see full source below. The key is to remember to reference "System.Windows.Forms"
at a project level.
Now add this as test code to the custom action project's "CustomAction1"
custom action code snippet (see code section towards bottom for full source):
// will launch the debugger for debug-build dlls
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
MessageBox.Show("hello world");
To get a standard setup GUI (for others who read this), add a reference to WiXUIExtension
as explained here (that is a step-by-step for creating a basic WiX project that compiles and has a GUI), and then inject this into your source:
<UIRef Id="WixUI_Mondo" />
I like to change <MediaTemplate />
to <MediaTemplate EmbedCab="yes" />
to avoid external source cab files (with this change cabs are compiled into the MSI).
If you don't have any components added, you can add this to include notepad.exe
in your MSI for test installation under directory INSTALLFOLDER
(just a trick to install something without having source files available - a source path that should resolve on any machine) - replace the whole "TODO" section - see full source below:
<Component Feature="ProductFeature">
<File Source="$(env.SystemRoot)\notepad.exe" />
</Component>
Now we need to declare the actual custom action and insert it into an installation sequence. Let's add this underneath the previous <Binary> element
:
<CustomAction Id="CA1" BinaryKey="CustomActions" DllEntry="CustomAction1"/>
<InstallUISequence>
<Custom Action="CA1" After="CostFinalize" />
</InstallUISequence>
<InstallExecuteSequence>
<Custom Action="CA1" After="CostFinalize" />
</InstallExecuteSequence>
Now build and test run the MSI. You should get numerous "hello world"
messages.
That is the overall "heartbeat"
of a C# / managed code custom action - the way I sometimes use them.
WiX源代码实际内容:现在,我们来综合考虑一下 - 记得替换所有GUID!:
下面的WiX源代码中,构建:
$(env.SystemRoot)
获取环境变量%SystemRoot%
,在大多数系统上将解析为C:\
(要列出环境变量,请打开cmd.exe
输入set
并按Enter
)。以下源代码因此应该可以在所有系统上编译而无需修改:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="DemoCA" Language="1033" Version="1.0.0.0" Manufacturer="test" UpgradeCode="0adf972a-5562-4a6f-a552-dd1c16761c55">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<MediaTemplate EmbedCab="yes" />
<UIRef Id="WixUI_Mondo" />
<!-- START CUSTOM ACTION CONSTRUCTS -->
<Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" />
<CustomAction Id="CA1" BinaryKey="CustomActions" DllEntry="CustomAction1"/>
<InstallUISequence>
<Custom Action="CA1" After="CostFinalize" />
</InstallUISequence>
<InstallExecuteSequence>
<Custom Action="CA1" After="CostFinalize" />
</InstallExecuteSequence>
<!-- END CUSTOM ACTION CONSTRUCTS -->
<Feature Id="ProductFeature" Title="AddingCSharpCustomActions" Level="1">
<ComponentGroupRef Id="ProductComponents" />
</Feature>
</Product>
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="AddingCSharpCustomActions"/>
</Directory>
</Directory>
</Fragment>
<Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<Component Feature="ProductFeature">
<File Source="$(env.SystemRoot)\notepad.exe" />
</Component>
</ComponentGroup>
</Fragment>
</Wix>
简要步骤:所需更改的简短总结:
MediaTemplate
设置为使用嵌入式cab(可选,对该示例的工作不是必须)。自定义操作代码:最后是实际的C#自定义操作测试代码 - 更新了Debugger.Launch,它将为调试版本的DLL启动调试器。然后,您可以将调试器附加到正确的源项目并逐步执行代码:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.Windows.Forms;
namespace CustomAction1
{
public class CustomActions
{
[CustomAction]
public static ActionResult CustomAction1(Session session)
{
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
MessageBox.Show("hello world");
session.Log("Begin CustomAction1");
return ActionResult.Success;
}
}
}
链接:
1.
** .NET Framework 3.5开发工具,**2.
** .NET编译器平台SDK,**3.
** 随附的几个依赖包:“C#和Visual Basic”以及“.NET编译器平台SDK”等等... 4.
Windows 7上已经存在.NET 3.5。**5.
** 安装WiX和WiX VS集成。 - undefined