I need to copy some xsl files to output directory in a folder named XSL when building. I set the Copy to Output Directory setting to Copy if newer on those files. The xsl files get copied correctly to the output folder when I build it locally. However, when deploying and running the code to reference those xsl files in test environment with Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, “XSL”), it does not find any files.
After a bit of investigation, I find that in the project file, it has the local path to the xsl files instead of the relative path. For example,
<None Update="C:\Dev\SampleProject\src\Sample\XSL\SomeFile.xsl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None></code>
That directory does not exist on the test environment so that explains why it does not work on the test environment. After a bit of digging around, I find that setting the Build Action to Content sets these directories in the project file relative. For example,
<Content Include="XSL\SomeFile.xsl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
The strange thing is I have copied to output directory in the past without setting the build action to content and it used relative path. Not sure why it happens to this solution. I’ve also tried it with a different solution and not setting the Build Action did use relative path.