Common Files in Visual Studio Solution - visual-studio

Many times I have seen Visual Studio solutions which have multiple projects that share source files. These common source files are usually out in a common directory and in the solution explorer their icon shows up with a link arrow in the bottom left.
However, any time I try to add a source file to the project that is outside of that project's main directory, it just automatically copies it into the directory so that I no longer have a shared copy.
I found that I can get around this by manually opening the project file in a text editor and modifying the path to something like "../../../Common/Source.cs" but this is more of a hack then I would like.
Is there a setting or something I can change that will allow me to do this from within the IDE?

Right click on a project, select Add->Existing Item->Add as link (press on small arrow on Add button)

Thanks #aku!
I knew this could be done, but I didn't know how to do this from Visual Studio. It shows up as a shortcut to the file and the csproj file generates the resulting XML like this:
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>CommonAssemblyInfo.cs</Link>
</Compile>
I've seen this technique commonly used for common AssemblyInfo files to keep a consistent version.

Related

Organizing project files in Visual Studio for better explorer view

I have a project in visual studio where there are several files arranged in folders (filters). Inside the project, everything is well organized, but as soon as I go to the explorer, I see that all the project files are in the same folder. How can I properly organize my project so that the repository doesn't look like a bunch of files in one folder?
I use Visual Studio 2022
I tried to manually decompose the files into folders, but when I did this, visual studio began to swear that it could not find the files
Can you share a screenshot of what you have and what you want?
I ask because there are two aspects to this.
The arrangement of projects within Solution Explorer
The arrangement of files within projects within Solution Explorer
The first can be controlled by adding solution folders. The structure is stored within the .sln file.
The second is generally a representation of the file system, depending upon your project type.
Also, make sure you're using the Solution Explorer view. Does this button appear clicked for you?
If so, you should unclick it. Solution Explorer can show you either the solution view (default, and what I refer to above) or the file system view.

Show all folders and files that are not added to the .csproj file

I have trouble finding out how to see or filter all files in the solution explorer, that are not added to the .csproj project file.
In the 'navbar' of the solution explorer, there is the option to show all files, what shows me also files that are not added to the .csproj file.
Is there a way to show or filter unadded folders or files in the solution explorer?
This would save me a lot of time. Because with every update this project gets, I have to look through the project file in the solution explorer and add every unadded folder or file by hand.
The "Show All Files" button does not appear if you are selecting the Solution itself. If you select an underlying Project, or any of the folders/files in the project, the button will display:
This button should be there in your solution. It is in my visual studio 2017
This is a case that just shouldn't happen if the project is being updated properly.
What this means is, that when someone updates the projects and adds new files, that person doesn't check-in the relevant changes to the .csproj.
As far as I know, there's no way to list all files that haven't been included.
Instead of trying to fix this problem, rather try to fix the underlying problem: files that are being added to the project, but not to the .csproj.

How to set up folder structure in Visual Studio

I am working on a project and I can see lots of folders which are dimmed and have dotted outline. I can expand them and each usually contains several projects inside. These are not hidden folders. Also, when I try to open projects into windows explorer, it opens up in a differently named folder than what it appears in Visual Studio.
I want to know what is it and how can this be done?
They are called Solution Folders and they are not bound to the folder structure on the disk. If you open your solution file with a text editor, you can see how they reference the projects.

Visual Studio dotted folder

In the Visual Studio (2010) Solution Explorer, what do the dotted "ghost" folders mean? I can still open them up and open contents within them, so how do they differ from the rest of the normal yellow folders? My reason for asking is that currently my project is missing a file that it expects to be in one of the dotted folders, so I was hoping that some insight into exactly what they mean would be helpful.
Thanks!
Also this can refer to a file/folder that is not included in the project. This can be included by right clicking and selecting 'Include In Project'.
It means it is showing you a file or folder that typically would be hidden by Visual Studio. For example, the bin folder isn't something you typically interface with directly - when you build in VS, it adds the files into the bin folder. So, you don't really need to have access to it as it doesn't contain any editable files. However, it does still exist within the directory.

Visual Studio 2010 - .Sln file when clicking on .csproj

I can open my project by double clicking on the .csproj file. It opens fine and it doesn’t generate a .sln. If I copy the same project to a virtual machine and do the same it opens but creates a .sln file.
I really don’t need a solution I would prefer to only work on a single project.
Am I missing something here?
Visual Studio always creates a solution. If it cannot find one in the same folder as the .csproj file then it will create one itself, based on what it can reverse-engineer from the project file content.
The solution is hidden by default if the solution only contains one project. Fixing this is recommended: Tools + Options, Projects and Solutions, General, "Always show solution" checkbox.
There is always a solution, even if you don't see it. There's a setting that determines whether the solution is visible when there is only one project.

Resources