Wixsharp: Dir.AddFile error

Created on 15 Jun 2017  路  3Comments  路  Source: oleg-shilo/wixsharp

Hello!
I add file to directory via Dir.AddFile function:
Dir installDir = new Dir(@"%ProgramFiles%\MyCompany\MyProduct");
installDir.AddFile(new File(@"readme.txt"));
var project =
new Project("MyProduct", installDir);

After installation I saw that readme.txt file wasn't installed.

Generated .wxs is:

< Directory Id="TARGETDIR" Name="SourceDir">
  < Directory Id="INSTALLDIR" Name="ProgramFilesFolder">

    <Component Id="Component.readme.txt" Guid="6f330b47-2577-43ad-9095-18615e463af3">
      <File Id="readme.txt" Source="readme.txt" />
    </Component>

    <Directory Id="INSTALLDIR.MyCompany" Name="MyCompany">
      <Directory Id="INSTALLDIR.MyCompany.MyProduct" Name="MyProduct">

        <Component Id="MyProduct_.EmptyDirectory" Guid="6f330b47-2577-43ad-9095-18611bb63af9" KeyPath="yes">
          <CreateFolder />
          <RemoveFolder Id="INSTALLDIR.MyCompany.MyProduct" On="uninstall" />
        </Component>

      </Directory>

      <Component Id="INSTALLDIR.MyCompany" Guid="6f330b47-2577-43ad-9095-1861a454b961" KeyPath="yes">
        <CreateFolder />
        <RemoveFolder Id="INSTALLDIR.MyCompany" On="uninstall" />
      </Component>

    </Directory>
  </Directory>

  <Component Id="TARGETDIR" Guid="6f330b47-2577-43ad-9095-18612df5f80e" KeyPath="yes">
    <CreateFolder />
    <RemoveFolder Id="TARGETDIR" On="uninstall" />
  </Component>

</Directory>
question

All 3 comments

Your problem is in your Dir instantiation code:
```C#
Dir installDir = new Dir(@"%ProgramFiles%\MyCompany\MyProduct");

If you examine the `installDir` under debugger you will see that its name is "%ProgramFiles%". Thus you were adding your readme to the directory which it two levels above the position you want.  And your XML clearly shows that.

A clumsy work around would be to access the desired dir as below:
```C#
installDir.Dirs[0].Dirs[0].AddFile(new File(@"readme.txt"));

The following is a better approach:
```C#
Dir installDir;
var rootDir = new Dir(@"%ProgramFiles%\MyCompany",
installDir = new Dir("MyProduct"));

installDir.AddFile(...
```
And arguably the best approach is a parametric constructor as shown in all code samples. These constructors were designed specifically to avoid the difficulties you are experiencing.

Thanks a lot!
Your example is very cool to me. I have to use AddFile because I add files dinamicly.

Great. And you can also lookup the dir you are interested in:
C# Dir installDir = project.AllDirs.First(x => x.Name == "My Product"); installDir.AddFile(...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Chrissyoung1223 picture Chrissyoung1223  路  5Comments

UweKeim picture UweKeim  路  5Comments

ayman-eltemsahi picture ayman-eltemsahi  路  4Comments

marcobeninca71 picture marcobeninca71  路  4Comments

JasShao picture JasShao  路  4Comments