Is it possible to dynamically add directories to a project?

Jul 16, 2015 at 8:47 PM
I'm trying to add a directory to and existing Project object, but I'm having some trouble getting it to work. I've attempted to do it like this:
project.ResolveWildCards(ignoreEmptyDirectories:true)
                .FindDir(@"%ProgramFiles%\myDir")
                .Dirs
                .Add(new Dir() { Name = "SomeDir" });
Obviously, this doesn't work, but I can't figure out why not. Is it possible to do something like this, and if so, how?
Coordinator
Jul 17, 2015 at 2:17 AM
Edited Jul 17, 2015 at 5:19 AM
It fails because Dirs is an array and you cannot change it. It is the same case as with the call myString.Replace(" ", "_") not changing the myString value.

You need to use Dir.AddDir extension method:
using WixSharp;
using WixSharp.CommonTasks;
...
var dir = project.ResolveWildCards(ignoreEmptyDirectories: true)
                 .FindDir(@"%ProgramFiles%\myDir");
dir.AddDir(new Dir() { Name = "SomeDir" });

//оr

project.ResolveWildCards(ignoreEmptyDirectories: true)
       .FindDir(@"%ProgramFiles%\myDir")
       .AddDir(new Dir() { Name = "SomeDir" });
Jul 17, 2015 at 8:06 AM
Hi,

It is also possible to extend the project.Dirs array:

Array.Resize(ref project.Dirs, project.Dirs.Length + x);
where x is a desired number....

and then you can just do something like:
project.Dirs[x] = newDir;

This should work. :)
Jul 17, 2015 at 12:12 PM
Ah, ok. That makes sense. Thank you!