Search code examples
c#visual-studio-2017projects-and-solutions

How to organise C# source files in subfolders?


BACKGROUND I'm mostly programming in embedded C/C++ but sometimes I have to do some C# programming for our API. For this I'm using Visual Studio 2017 to create an API DLL for our customers.

The C# API and our C/C++ firmware are using a common set of status codes. I have a Lua script that generates these codes to a .h (for C/C++) and a .cs (for C#) file so they always are in sync.

All source files that are shared across products and platforms are in a special project called "Common" (checked-in to Subversion).

When we create new projects and use any "common" file, we put them in a sub folder called "Common\" so we know that there is no point in messing with them. Subversion will check out these "Common" files as externals of a specific revision used by each project.

In C/C++ it's no problem at all to have source code organized in several levels of folders, all source files have a relative path to the root project folder.

THE PROBLEM So in this C# project I organize the source code as usual:

ProjectRoot\source.cs
ProjectRoot\Common\EStatusCodes.cs

In the ProjectRoot\ we have all .cs files for this C# project, and in ProjectRoot\Common\ are the external files from subversion's "Common" project.

So after the checkout of the external EStatusCodes.cs into the ProjectRoot\Common\ folder I add it to the C# project by "Add->Add Existing..." and then I point out the ProjectRoot\Common\EStatusCodes.cs file.

The file shows up in the Project but for some reason Visual Studio has COPIED the file form the ProjectRoot\Common folder to the ProjectRoot\ folder and is then using the copy! (The file's path in properties is set to the ProjectRoot\ folder.

So If we add more status codes to the "Common" project, this C# project don't get the update because Visual Studio now always use the copied version of the file from ProjectRoot\ and don't care if ProjectRoot\Common\EStatusCodes.cs has been updated.

I tried to add the Common folder to "Properties->Reference Path", but it still copies the file every time I add it to the project.

Is it possible at all to have source files somewhere else than in the C# project's root folder?


Solution

  • In the Add Existing dialog, there should be a small down arrow next to the Add button. If you click this, you'll see an option to "Add As Link". This will add the file as a reference link to the original file and any changes to the original will reflect in your project.

    enter image description here