简体   繁体   中英

How do I add project references to a wix project programmatically?

I am attempting to add references to a Wix project after it has been created programmatically. This is in an implementation of Microsoft.VisualStudio.TemplateWizard.IWizard . I know how to do it for EnvDTE.Project 's:

// Add project reference for projectA from projectB.
// Allows projectA to use classes from projectB.
var temp = (VSProject2)projectA.Object;
temp.References.AddProject(projectB);

But I can't figure out how to do it for an OAWixProject . Any help would be greatly appreciated. Thank you!

EDIT: I should mention that problems arise when I attempt the cast (VSProject2)projectA.Object when projectA is an OAWixProject .

Not sure about how you'd use WIX sdk dlls to edit the wixproj file, but you could always resort to editing the wixproj xml programatically. Looks like you'd add an ItemGroup element to the root Project element. The xml you'd need to inject would look something like:

<ItemGroup>
    <ProjectReference Include="..\MySolution\CustomAssembly.csproj">
        <Name>CustomAssembly</Name>
        <Project>{233e4372-895b-4c8d-99b0-f8314d907d66}</Project>
        <Private>True</Private>
        <DoNotHarvest>True</DoNotHarvest>
        <RefProjectOutputGroups>Binaries;Content;Satellites</RefProjectOutputGroups>
        <RefTargetDir>INSTALLFOLDER</RefTargetDir>
    </ProjectReference>
</ItemGroup>

The project guid is taken from the csproj of the referenced project.

Although the question was asked some time ago and the anwer might not be of any relevance for you anymore, I came up with the following solution:

Assuming you have a EnvDTE.Project representing a WiX project and you want to add a reference to another EnvDTE.Project , there is no need to cast to or work with the OAWixProject type:

void AddProjectReference(
    EnvDTE.Project wixProject,
    EnvDTE.Project projectToReference)
{
    var references = wixProject.ProjectItems
        .Cast<EnvDTE.ProjectItem>()
        .Select(x => x.Object)
        .OfType<VSLangProj.References>()
        .FirstOrDefault();

    if (references != null)
    {
        references.AddProject(projectToReference);
    }
}

So the difference to working with a VSProject is how the references instance is retrieved (using VSProject.References for VSProject as you mentioned).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM