4

In C# Project files as written by Visual Studio or SharpDevelop, files that are not located in the folder hierarchy of a project can be included with a <Link> element.1

My directory structure looks like this:

[SolutionDirectory]
 |- [MyProject]
 |   |- TestProj.csproj
 |- [File]
 |   |- somefile.txt
 |- TestProj.sln

Hence, in TestProj.csproj, I can add the following:

<ItemGroup>
    <EmbeddedResource Include="..\File\somefile.txt">
        <LogicalName>MyResource</LogicalName>
        <Link>somefile.txt</Link>
    </EmbeddedResource>
</ItemGroup>

This works flawlessly, and somefile.txt is embedded as a resource. The project tree is shown as follows by SharpDevelop 4.4:

project with linked file

However, to make things tidier, I would like to organize files like somefile.txt in a folder hierarchy within TestProj.csproj. I have tried the following:

Just adapting the <Link>:

<ItemGroup>
    <EmbeddedResource Include="..\File\somefile.txt">
        <LogicalName>MyResource</LogicalName>
        <Link>File\somefile.txt</Link>
    </EmbeddedResource>
</ItemGroup>

Declaring a folder of the desired name:

<ItemGroup>
    <EmbeddedResource Include="..\File\somefile.txt">
        <LogicalName>MyResource</LogicalName>
        <Link>File\somefile.txt</Link>
    </EmbeddedResource>
    <Folder Include="File" />
</ItemGroup>

Declaring the actual folder as a link:

<ItemGroup>
    <EmbeddedResource Include="..\File\somefile.txt">
        <LogicalName>MyResource</LogicalName>
        <Link>File\somefile.txt</Link>
    </EmbeddedResource>
    <Folder Include="..\File">
        <Link>File</Link>
    </Folder>
</ItemGroup>

All of these yield the same result: The folder structure in the project is shown as desired, however, the File folder is marked with a yellow exclamation mark warning sign, implying that something is not quite all right:

project with linked file in a folder; the folder shows a warning symbol

The only way I have found to make this warning symbol go away is to actually create an additional File folder in the project directory:

[SolutionDirectory]
 |- [MyProject]
 |   |- [File]
 |   |- TestProj.csproj
 |- [File]
 |   |- somefile.txt
 |- TestProj.sln

As gets visible here, this additional File folder would remain empty, and empty directories is only possible with workarounds for version control systems such as Git. (Possibly, the same restriction applies to some archive formats.)

Therefore, I am still trying to find a better solution: How can I link to files outside of the folder structure of my project and sort them into an otherwise empty folder hierarchy within my project?

Unfortunately, the MSBuild Schema Reference on MSDN does not even mention the <Folder> element, so I failed to find out anything about the issue via the docs.

1: In general, this serves for including files in several projects. In my particular case, I am working on a project that needs to reference a class library in the same solution, but it also needs to include the entire source code (including the project file etc.) of that class library as an embedded resource.

Community
  • 1
  • 1
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
  • Do you need all the source code files as individual embedded resources, or will 1 zip suffice? – Balah Jan 14 '16 at 14:55
  • @Balah: I have considered embedding the zipped sources as a single resource, but I'm not sure whether it's possible to configure the project files to embed a zip file as a resource that is zipped on the fly. I might have to work with pre-build events and call an external zip application, although that would introduce a dependency to have such a zip application on the path again ... possible, although not optimal. – O. R. Mapper Jan 14 '16 at 15:01
  • Ah I think you're going to have to have pre-build events and they can zip on the fly... although... you don't necessarily have to use an external zip application - iff you're using .NET 4.5 and above (using the ZipFile class). I had a similar problem that I solved some time back using MsBuild inline tasks and tweaking the build order. I'll post an answer once I find the code somewhere – Balah Jan 14 '16 at 15:11
  • @Balah: I'm considering whether to create a separate question for that, so you can add your answer there. Your solution could help me in my current situation, but it is considerably different to what I am asking here (and it's not nice for future visitors to find a question that asks exactly what they want - linking to folders in other locations - and find that the OP accepted an answer that solved their underlying problem, but not the problem explicitly asked about). – O. R. Mapper Jan 14 '16 at 15:17
  • I share your sentiment. Have you tried using [Shared Projects](https://www.visualstudio.com/news/vs2015-vs#VSIDE). I'm yet to try it myself but apparently "...the code and assets in the shared project are treated as if they were files linked into the main project..." – Balah Jan 14 '16 at 15:28

0 Answers0