Visual Studio Solutions Folder as real Folders
Asked Answered
I

20

180

I have a Visual Studio Solution. Currently, it is an empty solution (=no projects) and I have added a few solution folders.

Solution Folders only seem to be "virtual folders", because they are not really created in the Filesystem and files inside solution folders are just sitting in the same folder as the .sln file.

Is there a setting that i've overlooked that tells Visual Studio to treat Solution Folders as "real" folders, that is to create them in the file system and move files into it when I move them inside the solution into one of those folders?

Edit: Thanks. Going to make a suggestion for VS2010 then :)

Isobaric answered 5/11, 2008 at 23:54 Comment(5)
This is one of the most annoying Visual Studio quirksYearwood
How can I correctly deal with this quirk?Wiegand
Funny thing, Rider kind of have this feature (but folder has to be at the same path as the .sln file, and actual folder reference is not stored in the .sln file itself): jetbrains.com/help/rider/Extending_Your_Solution.htmlRubble
I am using VS 2017 and as far as I can tell, MS has not yet added a feature to allow adding entire folders to a solution folder - individual files must be added.Bibb
A dotnet tool to sync SolutionItems with a filesystem github.com/timabell/sln-items-sync (I made this after getting fed up with manually aligning them).Armillas
A
56

No special setting. I don't think it's supported.

You can create real folders in a "project" within the solution, but not in the solution itself.

Africah answered 5/11, 2008 at 23:58 Comment(5)
This still appears to be accurate as of VS 2017.Bibb
... and VS 2019Legislator
one of worst things about VS. Why don't MS keep Sln folders as an option, but ALSO allow REAL folders to be added to the solution. So annoying. The File System works, why reinvent the wheel (as a square).Nephritis
you can change the behavior for the project: https://mcmap.net/q/55245/-visual-studio-solutions-folder-as-real-foldersTieback
...and VS 2022 -Scutt
U
57

There is a workaround, that actually behaves as expected.

  1. Add a New or Existing Web Site to the Solution. (I usually create a new one.)
  2. Just make sure it's created inside your solution folder. (I sometimes even create a "link" to an external folder, e.g. 'Docs' or 'Marketing' on a network share. In that case it's ignored by Git of course.)
  3. Make sure to go to the "Project" settings or Configuration Manager to exclude this "Web Site" from Build and Deploy!

Done. Now Solution Explorer will reflect any change in the file system and vice versa (including subfolders).

I (miss)use it for specs, docs, PM and some DevOps scripts that are shared within the team. It's easy to choose, what to include in source control or not, and (if set up correctly) it doesn't conflict with build.

I know the feature is not intended for that use case, but except for the maybe misleading "Project" icon I didn't find any shortages to that hack yet. And there still are use cases where the classical (virtual) Solution Folders that VS provides, fit in the picture. What do you think?

Underarm answered 5/5, 2016 at 9:43 Comment(4)
Here are the full instructions: Right-click the solution -> "Add" -> "New Web Site..." -> (I chose "ASP.NET Empty Web Site"). After changing the location don't forget to append "\MyName" to the path, otherwise clicking "OK" will simply re-open the dialog. After that right-click your solution -> "Properties" -> "Configuration Properties" -> uncheck "Build" for the web project.Imbricate
Is this still working? using VS2015 but it doesn't work.Disgrace
In VS2017 perfectly creates a real folder but WebSite icon (dark circle) displayed in the Solution Explorer. Does anybody know how to fix that?Malaguena
Yes it works in VS2019, but still doesn't monitor filesystem changes. You must occasionally "Refresh" to see current files. If only C# projects weren't the only solution type to support the new, cleaner and clearer filesystem-glob-based .*proj format.Protuberant
A
56

No special setting. I don't think it's supported.

You can create real folders in a "project" within the solution, but not in the solution itself.

Africah answered 5/11, 2008 at 23:58 Comment(5)
This still appears to be accurate as of VS 2017.Bibb
... and VS 2019Legislator
one of worst things about VS. Why don't MS keep Sln folders as an option, but ALSO allow REAL folders to be added to the solution. So annoying. The File System works, why reinvent the wheel (as a square).Nephritis
you can change the behavior for the project: https://mcmap.net/q/55245/-visual-studio-solutions-folder-as-real-foldersTieback
...and VS 2022 -Scutt
J
51

In Visual Studio 2017, click on the "Solutions and Folders" icon in the Solution Explorer window. This button toggles from the virtual "solution" view into a "source view" that matches the layout of folders and files on the file system. When you add a new folder, the folder is physically created in the expected location. solutions and folders.

Jacoby answered 17/1, 2018 at 13:29 Comment(4)
This is useful but on "source view" you lost all the right click shortcuts on a project, i.e., "Manage NuGet Packages".Bielefeld
Why do only C++ solutions or projects behave differently compared to other languages?Jollification
This helped me, so i created a folder in the folder view where i wanted it, then i added a solution folder and added the project as a child of the folder. Doesnt really make sense to me, but this answer helped get meNagpur
This is exactly what I want. Thanks!Stearns
R
15

The chosen answer suggests it would be possible to use actual projects instead of solution folders, but does not really explain how. I guess what I'm describing here is possibly the least awkward way of achieving that... :-P

The problem with regular project files is that they eventually will be compiled by MSBUILD. And if you want have a project which only contains non-compilable files, that will be an issue.

But some time ago Visual Studio introduced a new project type: Shared Project (.shproj extension). This project type does not get compiled by default, but only when (and only if) it is referenced by another project.

So one part of the trick here is to use shared projects instead of solution folders. It's obviously possible to add a shared project that is never referenced by any other project, meaning we can avoid the issue presented above.

Then, by using <None Include="**/*" /> clause in the .shproj file, we can make it automatically reflect any new files and/or subfolders.

So basically do this:

  • Create a new folder in your solution.
  • Add a new .shproj file at the root of this new folder.
  • Reference the new .shproj in your solution.

For instance, in my case, I've created a DockerDev.shproj, so I can group some docker-related scripts that we run only in our development machines:

<?xml version="1.0" encoding="utf-8"?>
<!-- DockerDev/DockerDev.shproj -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="**/*" />
  </ItemGroup>
</Project>

This .shproj file will keep track of any file, in any subfolder of this new DockerDev folder in my solution.

As far as I could see, this solution works pretty much like what the OP requested: it will work as a non-compilable reference to a folder, and it will automatically reflect any changes made to it.

Rubble answered 14/4, 2019 at 13:47 Comment(4)
In my experience, this accumulates cruft as soon as an unsuspecting team member touches any file properties; after which you have duplicate files appearing in the navigation pane. Under some conditions every file ends up enumerated in the .proj file and then ugliness ensues. Although I dislike giving up, I still prefer the 'Add Existing Web Site' approach.Protuberant
@Protuberant I never experienced the behavior you're mentioning, but "absence of evidence is not evidence of absence", so there's that. But I use shared projects on a daily basis, so I think that also counts. Regarding Web Site Projects (WSP), they could also work, I guess, been a long time I don't use it.Rubble
This is handy. I tried adding a shared project and it worked. Mine is for sql scripts that the code does not run.Pertinacity
Yeah @PaulMcCarthy, 4 years later and I still think this is the best approach. Also, I still never got any "duplicate files" whatsoever, so I'm not sure what shannon's issue was (or even if it was really related with this solution at all).Rubble
M
10

You can just sync your new solution folder nesting level and also name with the actual filesystem folder and it works like a charm!

Existing project :

  • Create the actual folder
  • Create the solution folder with the exact same name
  • Copy your project folder into the new folder (Actual file system)
  • (in solution explorer) - Righ-click on same folder
  • Add => Existing project

Add new project :

  • Create your solution folders
  • (Right-click on solution) => Add new project
  • Change the Location address under the project name you want to add, to the exact same address in your solution folders
Mcewen answered 18/9, 2021 at 19:23 Comment(5)
By "Place your project in it," do you mean, "Place your project folder (with files on it) into the 'actual folder' you just created in the file system"?Intelsat
@Intelsat I made some edits. Hope it helps.Mcewen
I can confirm this works in VS2019! Those of us who keep changing our minds about folder structure end up with this problem a lot. I wish it were easier. Note that If you already have the projects in source control, you'll have more work to do when you check in after this.Favianus
@Favianus That's right, and also all files become their first version for source control.Mcewen
I used the steps for Existing folder, worked perfectly fine . Ty!Idiographic
M
8

Sara Ford contributed a Macro to add do this. In Visual Studio 2010, if you open your Macro Explorer, you will see a macro called "GenerateSlnFolderOnDirStructure." This will automate the creation of the Solution Folders and add the files.

Morph answered 29/8, 2012 at 18:59 Comment(0)
L
7

Folder To Solution Folder By Cecilia Wirén - CeciliaSHARP

Remove the hassle of adding several files to solution folder. Just use the context menu for the solution and just below the option of creating a new solution folder you now find 'Add Folder as Solution Folder'. This will create a solution folder with the same name as you selected and add the items inside of that folder to the solution folder. This will not move the files on disk.

Lorineloriner answered 27/1, 2017 at 19:36 Comment(0)
D
6

For C# in Visual Studio 2019 I used this way (Seems to be similar to this answer, but that didn't work at least in C# solutions)

  1. In solution explorer click on switch views

enter image description here

  1. Choose folder view

enter image description here

  1. You can add individual folders to the solution

enter image description here

  1. To get back to the regular view of solution explorer just click switch views again and choose the solution.

There seems to be a limitation using this way (comment from @montonero):

... just open a solution with multiple projects and try to move the projects to some other real folders through the folder view. The issue is VS doesn't update paths to projects in a solution file

Dedans answered 10/4, 2021 at 10:53 Comment(6)
I'm not sure if this is new VS 2019 functionality, but this did provide what I needed. I wanted to have my file system and project in sync ALWAYS. This view is exactly that. If a file is created via the IDE, it is added to the file system in the appropriate folder (as is selected in the IDE). If a file is deleted, the file system is updated accordingly.Kimber
If you try to reorganize projects in this view, you'll get a solution with broken project references.Atmolysis
@Atmolysis I used this way in several complex projects, without any problems. Do you have an example?Dedans
Sure, just open a solution with multiple projects and try to move the projects to some other real folders through the folder view. The issue is VS doesn't update paths to projects in a solution file.Atmolysis
@Atmolysis thanks, I never used is this way and edited my answer nowDedans
In VS 2022, the 'Solution Explorer – Folder View' does not show source-control status icons for files. It also does not have the source-control options when you right-click a file in this view.Catabolite
F
5

No, it's not supported. As you suspected, solution folders are simply virtual subentries in the .sln file, nothing to do with the file system.

Fidget answered 6/11, 2008 at 0:4 Comment(0)
H
4

Create an empty solution then open .sln file in an editor and put these lines of code after MinimumVisualStudioVersion

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9D8C3BB1-AEDB-4757-8559-995D12A4E6D0}"

open the solution in vs and you should add the same folder to it now you can see the folder and add a project to it you have a real folder in windows and a virtual one in vs

be sure that you created the projects with that path

Hanschen answered 19/12, 2020 at 9:5 Comment(1)
the first GUID is the key here.Crosseye
C
3

Visual studio has no support for this. I made an extension that does something similar for VS2013 though. It maps solution folders to physical folders on your hard drive, though the mapping is one way (from hard drive to solution). That means a solution folder's contents will reflect the hard drive folder's contents, and not the other way.

With that out of the way, the extension may still be useful. It has support for mapping solution folders to physical folders, filtering files and directories based on regex, and remembering mappings in your .sln file. Properties are non-intrusive so developers without the extension can still open the sln and not be affected.

Hosted on visual studio gallery: https://visualstudiogallery.msdn.microsoft.com/69e19ea6-4442-4cb6-b300-044dd21f02bd

Edit: Uploaded to bitbucket. Now open source. MIT license. https://bitbucket.org/LSS_NorthWind/physical-solution-folders

Corbel answered 4/5, 2015 at 15:56 Comment(0)
S
3

Create "Solution folder". This will create logical folder, but not physical one. Right click to the solution folder and open a new project dialog. But before you click OK, you have to change a project location to your desired physical folder and VS will create it and place the project inside.

Scan answered 15/8, 2017 at 15:4 Comment(0)
L
2

You can add real folders by choosing "Add new filter" for a Visual Studio project file. You can also do "Add new filter" under an existing folder. Once the folder is created, rename it and add source or header file or whichever suits your project. This is one way I know which lets us create real folders through the Visual Studio IDE.

Lemuel answered 3/4, 2012 at 1:6 Comment(2)
This feature is specific to C++ projects.Thrasher
This solution also applies to VS Projects - the question is about Solution level folders.Bibb
C
2

I've wanted this feature a few times myself, but at the end of the day, you really do NOT want the ability to do this. Think of your Solution (file) as as the root of a web application and think of Solution folders as Virtual Directories (literally and functionally). The contents of a web virtual directory could be physically on a different server altogether. Where Visual Studio muddled up the solution folders concept is by allowing you to create new files inside the folder. You should always "Add Existing" when adding content. When you add existing, it creates a link to the source location of the file.

But as for the reason you do not want solution folders to behave like "physical" folders is because your solution layout may not necessarily use the same convention as your source control layout. Solution folders allow you to customize the hierarchy of your projects so that you can group projects and items together any way you like, and then decide you don't like it and change it again without having to go through the nightmare of moving source control items around and irritating the rest of your team.

Cystocarp answered 7/9, 2014 at 22:39 Comment(17)
This is the right answer - create the file on disk in a folder with the same name as the virtual folder, then add the file in VS using 'Add existing'.Kan
They could have easily implemented both virtual and physical folders. It is clearly an overlook. See reference: every other IDE, ever.Thrasher
Tamir, I think that would be a nightmare for those of us that have to support mid/junior developers. Obviously VS supporting just one of those has proven confusing to a lot of people, I can't even imagine the chaos of VS supporting both "ways" would cause. I'd rather not spend half my time cleaning up solution directories. As to what other IDE's support...irrelevant.Cystocarp
I don't understand at all why the concept of (IIS) Virtual Directories has anything to do with solution folders. As for the source control argument, I don't see the problem. Why would file moves irritate the rest of your team? It's a common operation. And why would you want the layout of the files in source control to be different than the physical layout?Conciliar
@stijin. Don't down-vote just because you don't understand. What is confusing about a folder that doesn't really exist (virtual) and its contents that could be physically located on a separate drive from the rest of the project and solution files? Seems simple to me. And moving around projects in SCM causes problems all the time. Especially for users who cannot understand concepts like virtual folders. And I may want to group projects together in one solution that are in separate repositories in SCM. Thus, physical vs virtual.Cystocarp
This is the best answer that I've seen yet! Nice.Diastasis
This worked for me, and TFS picked them up correctly as well. TFS will only pick them up if you add your physical solution folders to the solution folder.Bombay
Physical solution folders would make it easier (albeit just a bit) to create modern folder structures like NancyFx's, where multiple projects fall into categories like src, test, tools, etc. You'd definitely want to make that decision at the project outset to your point about irritating the team, but that's true of most any architectural decision.Wey
-1 My usecase is this: sometimes we just want to add certain documents to the solution. They will not be built, but they are kept into the source control. We usually have a special folder for them. I would like to have that folder in my solution - not the files it contains, but the folder itself. Yes, there are ways of bypassing this limitation, but they are not optimal. Have an actual reference to a folder in the solution would simply work.Rubble
@Rubble So you downvote me because VS doesn't support your requirements? Nice.Cystocarp
@PaulEaster, no, I downvoted you because you simply rationalized the question away, like it was something not important. You failed to recognize the usefulness of what was requested, as now you're failing to recognize the honest feedback we're giving to you.Rubble
@Rubble Um, no I answered the question, and in addition to answering the question correctly, I offered my professional opinion as to why Microsoft decided to make it that way. This wasn&#39;t a philosophical debate. By the way, the question was: &quot;Is there a setting that i&#39;ve overlooked that tells Visual Studio to treat Solution Folders as &quot;real&quot; folders, that is to create them in the file system and move files into it when I move them inside the solution into one of those folders?&quot; Edit: The answer is "no", btwCystocarp
@PaulEaster I understand you feel this way, but the community should be the one to decide if your answer is correct or not, not you. Also, try to give positive feedback in the future. Please read the code of conduct.Rubble
@rsenna, what are you talking about? You downvoted me just because you didnt like my explanation as to why the answer is no. Also, this question applies to VS 2008. This convo is no longer worth the effort. LolCystocarp
@PaulEaster What I'm trying to say is that most of your answer is about why we should not want to add folders to a solution file anyway. But there are plenty of reasons to want that - my comment presented one usecase, I'm sure I can come up with others. In the end, besides the obvious "no" (that everybody else here already stated, including the chosen answer), what you're giving us is just an opinion.Rubble
@Rubble I understand your point and even agree with it. What I&#39;m trying to say is that in Visual Studio 2008, if you could &quot;enable&quot; an option to do that (there is a registry hack for it) doing so could very likely muck up your source control. Most likely because of its (VS 2008's) lack of decent git compatibility.Cystocarp
Have always disagreed with this stance, which also has seemed to have been the MS stance over the years. VS (any version) does not hold exclusive rights to edit or compile my code, for any language, but especially for NodeJS, Angular, C++, and other projects that are commonly the domain of non-MS toolsets. We would like our IDE to build bridges, not barriers. I'll skip the downvote, but mainly because I see it offended you previously, not because the question and answer are version- or year-specific.Protuberant
M
1

Note: Yes this is possible you can create a folder on root but its lil bit tricky....

By giving some extra efforts you can do it How? Lets follow the step--

  • 1-Create Folder eg: "newfolder" on root (where your .sln file reside).
  • 2.Copy and paste your projects inside the folder.
  • 3.go to your sln file and find moved projects and append newfolder\ in moved project's address.
  • 4.Save sln file.
  • 5.Open your project and Commit the repository in git or so...
  • 6.Take the repository on fresh location.

    YOU are done...

if still you are not able to see your folder -----

  • 1.Add a solution folder xyz.
  • 2.Open sln file and change that folder name with your folder name.

Congrats you are done..

If you face any problem just write me for help..

Monti answered 9/2, 2017 at 11:26 Comment(1)
This is an instruction on how to move project folders, while OP asks how to show additional directory under solution without listing all the files statically.Shepherd
T
0

The folder created underneath the solution will be virtual as said. Maybe this might be called a workaround but you can physically create the folder on disk either before or when you add new item/project and Robert should be a sibling of your dad.

ps- on closer look maybe i should explain "bob's your uncle" means your fine/sorted.

Tuberculosis answered 6/11, 2008 at 0:26 Comment(0)
D
0

I have a bit of a workaround for this (it's not great, but it works).

  1. Create a folder in your solution (i.e. "Contoso")
  2. Right click on the solution and then click "Open Folder in Solution Explorer"
  3. Create the physical folder (i.e. "Contoso") in the solution directory
  4. Copy/Create files in the physical folder.
  5. Drag the files into the virtual folder in the solution explorer.

It's not great because you will need to manually maintain the file references, but it works for me.

Decastyle answered 10/11, 2015 at 11:45 Comment(0)
T
0

Yes, it is possible in Visual Studio 2019 for the project

Though this is an old topic, I will add my answer, because I had the same issue and searched for a solution but it seemed that everyone is 100% sure that there is no way to do it. So I started to experiment with VS 2019, tried a lot of settings, and eventually figured the way out.

1 Button :D

You only need to click 1 button - Show All Files, and you will see the physical structure of your Visual Studio Solution:
VS 2019 - Show All Files Button

Now you can add files and folders to the project and they will be added to the file system (physically)

Right-click on your project → AddNew Folder
Note that the option changed from New Filter to New Folder
VS 2019 Add Folder Menu

My recommendation for C++

  1. Create a root folder inside your project's directory which will contain all the application related stuff (code, headers, data, libs... ). I name it Project
  2. Add subfolders as you'd like to structure your code. I prefer the following layout: include, src, data, libs, etc

Recommended VS 2019 Project Structure

  1. Now setup Visual Studio to recognize these folders as headers and sources directories.
    1. Click on your project in the Solution Explorer. Note, in my case, it is CrazyDemo, not the
      Solution 'CreazyDemo' (1 of 1 project)
    2. Go to the project properties menu: ProjectProperties
    3. Open Configuration PropertiesVC++ Directories tab
    4. Edit Include Directories and set to $(ProjectDir)/Project/include;$(IncludePath)
    5. Edit Library Directories and set to $(ProjectDir)/Project/libs;$(LibraryPath)
    6. Edit Source Directories and set to $(ProjectDir)/Project/src;$(SourcePath)

VS 2019 Recommended Project Properties

  1. You may use the Scope to This option if you want to focus on 1 project. Just right-click on the Project folder and press Scope to This
    • Note that after this action, in order to open Project Properties you need to click on any of your project files (like main.cpp in the example) and then click on the editable client area (like when you want to change the code) and only after that you'll be able to see the
      ProjectCrazyDemo Properties option. [Visual Studio is Insane 🤦‍♂️]

Finally, you may have a project like this

VS 2019 Recommended Project Example

Tieback answered 2/2, 2021 at 6:56 Comment(5)
Is it possible to save this was a template? Or, do I have to go through all these steps every time I create a new project within a solution?Intelsat
I don't know, never tried to use templatesTieback
This works only for folders inside a project. You can't make this way a folder to combine several projects.Atmolysis
@Atmolysis yes, as I stated in the first sentence of my answer :)Tieback
Well, I see the word "project" but I don't see that you've emphasized in any way that this is ONLY for projects so I've made it clear.Atmolysis
D
0

This is now supported in Visual Studio 2022. It is now a hybrid folder.

  1. If you drag files from a project into it, it is a virtual reference to that file in the solution folder.
  2. If you right click the solution, and select add -> new item, it will create a file system link to a real file in the root.
Dimeter answered 16/10, 2023 at 16:16 Comment(0)
A
0

Install and run the open-source sln-items-sync dotnet tool as follows:

dotnet tool install --global sln-items-sync

sln-items-sync --solution some-solution.sln [files and folders to add ...]

e.g.

sln-items-sync --solution some-solution.sln .github/ README.md .editorconfig

Tool source/website: https://github.com/timabell/sln-items-sync


I created it as an xmas hols project as my gift to the microsoft devs of the world because I also got fed up after 15+ years of manually syncing the folders.

Armillas answered 13/1 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.