使用WiX DTF获取组件代码
本文关键字:组件 代码 获取 DTF WiX 使用 | 更新日期: 2023-09-27 18:29:28
我一直在尝试找到一种方法,在给定产品代码的情况下获得Windows Installer组件代码。(实际上,我正试图使用一个不包含组件代码的快捷方式来获取已安装产品的组件路径,但这是一个更长的故事。)
我遇到了WiX DTF(Microsoft.Deployment.WindowsInstaller)程序集,这看起来是完成MSI工作的好方法。当我写下以下代码时,我满怀希望:
Session product = Installer.OpenProduct(productCode);
ComponentInfoCollection components = product.Components;
ComponentInfo component = components.FirstOrDefault();
我希望我能从ComponentInfo获得组件代码。不幸的是,我没有办法做到这一点。
是否可以使用WiX DTF类获取组件代码?(或者任何其他方式…)
我不理解为"产品"获取"组件"的愿望,因为这不是1:1的关系。也就是说,这可以满足你的要求:
const string PRODUCT_CODE = "{EBBD327E-F220-4567-88F8-CEE3BE560F81}";
var comps = from c in ComponentInstallation.AllComponents
where c.Product.ProductCode == PRODUCT_CODE
select c;
string componentId = comps.FirstOrDefault().ComponentCode;
我发现可以使用以下方法获取组件代码:
private string GetComponentIdFromMsi()
{
using (Session product = Installer.OpenProduct(_productCode))
using (Database database = product.Database)
{
var featureComponentsRecords = GetFeatureComponents(database);
var featureComponentsRecord = featureComponentsRecords.Single(x => x.Feature == _feature);
var componentRecords = GetComponents(database);
var componentId = componentRecords.Single(x => x.Component == featureComponentsRecord.Component).ComponentId;
return componentId;
}
}
private IEnumerable<FeatureComponentsRecord> GetFeatureComponents(Database database)
{
var list = database.ExecuteQuery("SELECT `Feature_`, `Component_` FROM `FeatureComponents`");
const int columnCount = 2;
const int featureOffset = 0;
const int componentOffset = 1;
int rowCount = list.Count / columnCount;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
var rowOffset = rowIndex * columnCount;
yield return new FeatureComponentsRecord((string)list[rowOffset + featureOffset], (string)list[rowOffset + componentOffset]);
}
}
private IEnumerable<ComponentRecord> GetComponents(Database database)
{
var list = database.ExecuteQuery("SELECT `Component`, `ComponentId` FROM `Component`");
const int columnCount = 2;
const int componentOffset = 0;
const int componentIdOffset = 1;
int rowCount = list.Count / columnCount;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
var rowOffset = rowIndex * columnCount;
yield return new ComponentRecord((string)list[rowOffset + componentOffset], (string)list[rowOffset + componentIdOffset]);
}
}
private class FeatureComponentsRecord
{
public FeatureComponentsRecord(string feature, string component)
{
Feature = feature;
Component = component;
}
public string Feature { get; private set; }
public string Component { get; private set; }
}
private class ComponentRecord
{
public ComponentRecord(string component, string componentId)
{
Component = component;
ComponentId = componentId;
}
public string Component { get; private set; }
public string ComponentId { get; private set; }
}