910

The issue is simple really. Instead of creating folders in Visual Studio, I create a directory structure for my project on the file system. How do I include all the folders and files in a project, keeping the structure?

If I "Add Existing File" on a folder named Services and navigate to a file in the directory structure .. Services > AccountManagement > CreateAccount.cs, it appears in Visual Studio like so: Services > CreateAccount.cs. I do not want this.

I have an entire directory structure worked out already, as I am mimicking our client developers using the same structure for organization. How do I add all the folders and files to the project in Visual Studio? Or do I have to do what most Microsoft users do and "put up with it" and recreate each and every folder through Visual Studio?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
  • 3
    possible duplicate of [How to "Add Existing Item" an entire directory structure in Visual Studio?](http://stackoverflow.com/questions/57776/how-to-add-existing-item-an-entire-directory-structure-in-visual-studio) – nawfal Aug 07 '13 at 08:51
  • In 2019.. https://stackoverflow.com/a/45806856/2864740 – user2864740 Nov 23 '19 at 01:52

12 Answers12

1562

You need to put your directory structure in your project directory. And then click "Show All Files" icon in the top of Solution Explorer toolbox. After that, the added directory will be shown up. You will then need to select this directory, right click, and choose "Include in Project."

enter image description here

enter image description here

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Gant
  • 29,661
  • 6
  • 46
  • 65
  • 184
    Is there a way to do this with a directory (tree) _outside_ of the project directory? – imre May 01 '11 at 18:36
  • 4
    Is there a way to do this for a folder that is NOT a subdirectory of where your project is? i.e.: Have E:\ProjectX\project.vcproj include the folder E:\Common\*.cs ?? – Matt Connolly Feb 28 '12 at 01:00
  • 4
    Almost works. I can see the directory tree, and add multiple files, but it won't let me add a whole directory including sub-trees. – ManicBlowfish May 09 '12 at 16:44
  • 6
    The problem is that it ONLY WORKS if you copy the directory tree in your Visual project through the Windows explorer. How can you do if you just want to reference some folders and files contained in an other project on your computer ? – hico Aug 07 '13 at 10:16
  • @RaduSimionescu Untrue. You can symlink or hardlink an external source, and then follow the steps above. mklink /d LinkDirectoryInProject MyAlreadyExistingDirectoryTreeOutsideProject – John Vance Feb 20 '14 at 23:50
  • 7
    Only way I know of to do this outside the project is using a symbolic link... see my answer: http://stackoverflow.com/a/26537736/835561 – Edyn Oct 23 '14 at 21:25
  • 1
    All my files were added as a flat list after this. All structure is lost in the other view. - Still better than nothing. – Notinlist Mar 02 '16 at 09:28
  • This is great for small projects but VS struggles to handle our project with over 12000 files included. – Organic Jun 23 '16 at 15:09
  • Visual Studio will hang if your project is too large to "Show All Files", so Todd Smith's answer worked best for me – cph2117 May 09 '17 at 21:17
  • if your files are elsewhere, and used between multiple projects, consider making them into a separate project to include in your solution. – Nyerguds Feb 03 '18 at 17:45
  • In Visual Studio 2017 you can use Folder View: https://stackoverflow.com/a/49279856/1854557 – Siavash Mortazavi Mar 14 '18 at 14:09
  • If you want to add existing codes outside your project without copying, see my answer at the bottom of this post. – davidj Feb 14 '19 at 18:49
  • If you've just created the folder then click the refresh button (two to the left of 'Show All Files') – d219 Feb 14 '20 at 15:32
  • I forgot about this option since I've been working in .Net Core for too long. Suddenly I had to create a .Net Framework version as well, and I was at a loss how I added a file within a folder .. – Harald S. Hanssen Mar 27 '20 at 13:15
90

You can also drag and drop the folder from Windows Explorer onto your Visual Studio solution window.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Todd Smith
  • 17,084
  • 11
  • 59
  • 78
  • 16
    This does not work in my installation of VS 2010 on Vista Ultimate 64 bit. – John Melville Apr 06 '11 at 03:14
  • 14
    No, does not work in my VS 2010 installation on Windows 7 either. – AH. Nov 15 '11 at 10:30
  • 10
    Confirming this doesn't work in VS 2010 (non-Administrator mode) too – David Gardiner Mar 23 '12 at 06:24
  • 10
    I don't think you guys are doing it right. VS 2010 does support folder drag n drop. I do it all the time to include my tool-kits. You have to make sure that you drop into the project tree. The solution panel and the solution node itself will not receive the drop. The OS is irrelevant here, but for doubters, I use Windows 7 64 bit. – Gavin Williams May 31 '12 at 04:34
  • --------------------------- Microsoft Visual Studio --------------------------- Folders cannot be dropped or pasted as solution items. Choose an individual document instead. --------------------------- OK --------------------------- – SarjanWebDev Oct 15 '12 at 00:37
  • 4
    "Solution Items" are different than adding items to a project. Solution Items are a special folder within the VS solution. – Todd Smith Oct 16 '12 at 00:11
  • 2
    Old question, but this is the answer I found, so: Note that, if running VS as administrator with the standard annoying Windows ACL stuff enabled, you may not be able to drag anything into the IDE. – 3Dave Sep 28 '13 at 22:11
  • 2
    Works in VS 2013, but keep in mind, this copies the entire folder into your project directory. – Alex Klaus Dec 02 '13 at 00:41
  • Confirmed it works in VS Express 2013. As @Klaus pointed out, dragged folders should be dropped in your project in the Solution Explorer. – nigong Oct 01 '14 at 20:13
  • in VS2015 works if dragged straight to project Icon. – user2136963 Feb 28 '16 at 16:06
  • 2
    I will copy source files not in the project directory, to the project directory.. not what I intended :) – Emile Vrijdags Apr 01 '16 at 10:11
  • Why doesn't this work in Visual Studio 2015 community edition and Windows 10 pro? – Mona Jalal Jun 05 '17 at 19:44
  • When you drag, hover over your solution folder. The "cannot copy" mouse indicator will change to "can copy" when you are hovering over a valid destination. This works fine. It is a standard windows drag and drop operation for many files in folders... – pollaris Sep 29 '17 at 16:18
  • @JasonHitchings drag and drop works only in folder view and there is no Include in project. I am working on Database project – dejjub-AIS Sep 06 '19 at 13:22
66

In Visual Studio 2015, this is how you do it.

If you want to automatically include all descendant files below a specific folder:

<Content Include="Path\To\Folder\**" />

This can be restricted to include only files within the path specified:

<Content Include="Path\To\Folder\*.*" />

Or even only files with a specified extension:

<Content Include="Path\To\Folder\*.jpg" >
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 4
    I found this helpful because I could then include a single tag for "copy always on build", rather than needing to annotate every individual file (as was the case in the accepted answer) – Gordon Bean Dec 03 '16 at 00:19
  • 2
    this is the best solution when you want add large hierarchy folder while Visual Studio is not responding & get hang. – Gowtham Alan May 29 '19 at 09:11
  • 5
    For those looking for where to add this, it's the file `.csproj` file. For example, `Product.Feature.Web.csproj`. You'll need to add something like `. – Gene Parcellano Jul 18 '19 at 14:15
  • 1
    How do you do these in VS 2022? – Alexander Oct 05 '22 at 11:32
  • 1
    I tried that in VS2019 and got `warning  : Wildcards in project items are not supported in VC projects and can cause unexpected behavior, instability and performance degradation in the Visual Studio IDE. Please refer to https://aka.ms/cpp/projectwildcards for supported options.` – Gabriel Devillers Feb 06 '23 at 16:30
35

Copy & Paste.

To Add a folder, all the sub-directories, and files we can also Copy and Paste. For example we can:

  1. Right click in Windows explorer on the folder, and Copy on the folder with many files and folders.

  2. Then in Visual Studio Solution explorer, right click on the destination folder and click paste.

  3. Optional add to TFS; Then in the top folder right click and check in to TFS to check in all sub-folders and files.

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
Catto
  • 6,259
  • 2
  • 52
  • 55
  • This worked for me but drag and drop didn't work in VS14 and Windows 10 pro. – Mona Jalal Jun 05 '17 at 19:47
  • 1
    drag and drop may not work unless you are running VS as administrator – RThomas Jul 04 '17 at 18:49
  • This worked for me in 2023 with VS2022 when adding Identity scaffolding crashed and I needed to import a directory structure from a newly created skeleton solution. – camelCase Jun 28 '23 at 17:38
20

You can use a symbolic link. This makes modifying the file in one project modify it in the other (as it's actually the same file).

To do this:

  1. Open cmd prompt as administrator
  2. mklink /d [current project directory name] [directory in other project it should point to]

This has it's drawbacks and pitfalls, but I use it on occasion for duplicate libraries that need different names.

Edit for Anoop: Steps to add to Visual Studio:

  1. Create link in the project folder using the steps above.
  2. In Visual Studio... select project in Solution Explorer.
  3. At the top of Solution Explorer... click the Show All Files button (may need to click it twice if already active).
  4. The link will now show in your project... right-click and choose Include In Project.

These are the steps I follow and works for a couple different projects.

Edyn
  • 2,409
  • 2
  • 26
  • 25
  • 1
    Note: mklink will not work on a FAT file system. It is for NTFS. – Adrian Dec 29 '14 at 11:38
  • Source control will wreak havoc with Symbolic links. Git needs admin privileges or just checks in the file. – CAD bloke Jan 10 '16 at 00:33
  • @CAD Could you elaborate? I tend to avoid symlinks, so I haven't tried using them with Git, but I do know the Git installer mentions symbolic link support. – Sinjai Aug 21 '17 at 20:27
  • @Sinjai Some GUI git clients will turn a symbolic link into the actual file (I presume because they don't run with admin rights). I avoid symlinks in git mostly because it can be inconsistent. Also, you can't be sure the symlink target is on all the machines to git repo is cloned to. I belive a git repo should be self-contained with no mystery dependencies. – CAD bloke Aug 21 '17 at 23:15
16

To expand on Yuchen's answer, you can include files and paths as a link. This is not the same thing as adding the existing items because it doesn't make an extra copy in your project's folder structure. It is useful if you want one canonical folder / file etc to be used in a lot of different places but you only want to maintain one version/copy of it.

Here is an example of what you can add to a *.csproj file to create the link

<Compile Include="$(Codez)\z.Libraries\Common\Strings\RegexExtensions.cs">
    <Link>Helpers\RegexExtensions.cs</Link>
</Compile>

<Compile Include="..\..\z.Libraries\MoreLINQ\MoreLinq\ExceptBy.cs">
    <Link>Helpers\ExceptBy.cs</Link>
</Compile>

<Content Include="C:\Codez\Libs\Folder\OtherFolder\**\*.*">
    <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

$(Codez) is a Windows Environment variable I defined, you can use the built-in Environment variables in the same manner.

The last example group is a bunch of content files I need in the final output. See https://stackoverflow.com/a/11808911/492 and other answers & links there for more on that.

More MSBuild info at https://msdn.microsoft.com/en-us/library/bb629388.aspx

CAD bloke
  • 8,578
  • 7
  • 65
  • 114
16

In Visual Studio 2017, you switch between Solution View and Folder View back and forth. I think this is a better option, because it will keep the solution cleaner. I use this to edit .gitignore, .md files, etc.

Solution View and Folder View

Siavash Mortazavi
  • 1,382
  • 1
  • 14
  • 18
  • 9
    The question is on how to add an existing folder to the solution, not just to view folders. – CShark Aug 01 '18 at 10:18
  • 1
    This is a really good option to see the actual structure, to open a folder in file explorer etc - but unfortunately you can't add an existing folder tree from the folder view. That is only possible in the solution view, as [Gant](https://stackoverflow.com/a/392477/1016343) has described it. – Matt Aug 02 '18 at 14:49
4

I think I found a way to do this with the Compile Include=".\Code***.cs" What I wanted is to include code recursively under my Code folder.

Here is the project file sample.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0" DefaultTargets="BuildTarget">
    <PropertyGroup>
        <OutputType>Library</OutputType>
    </PropertyGroup>
    <PropertyGroup>
        <StartupObject />
    </PropertyGroup>
    <PropertyGroup>
        <RootNamespace>Autogen</RootNamespace>
    </PropertyGroup>
    <ItemGroup>
        <Compile Remove="@(Compile)" />
        <Compile Include=".\Code\**\*.cs" />
    </ItemGroup>
    <Target Name="BuildTarget">
        <Message Text="Build selected" Importance="high"/>
    </Target>
</Project>
Pang
  • 9,564
  • 146
  • 81
  • 122
RJ Thompson
  • 126
  • 7
2

In Visual Studio 2013, I couldn't get "Include in Project" to work when right-clicking on a folder. What did work is expanding the folder, selecting all the files then choosing "Include in Project". It was quite tedious as you have to do each folder one by one (but at least you can do all files in each folder in one go), and it appears to store the file path (you can see this by viewing properties on the file and looking at the "Relative Path" option.)

I am hoping to use this to deploy some data files in a Visual Studio Installer project, and it seems to pick up the included files and preserve their paths.

Malvineous
  • 25,144
  • 16
  • 116
  • 151
2

Visual Studio 2017 and newer support a new lightweight .csproj format which has come to be known as "SDK format". One of several advantages of this format is that instead of containing a list of files and folders which are included, files are wildcard included by default. Therefore, with this new format, your files and folders - added in Explorer or on the command line - will get picked up automatically!

The SDK format .csproj file currently works with the following project types:

  • Class library projects

  • Console apps

  • ASP.NET Core web apps

  • .NET Core projects of any type

To use the new format, create a new .NET Core or .NET Standard project. Because the templates haven't been updated for the full .NET Framework even in Visual Studio 2019, to create a .NET class library choose the .NET Standard Library template, and then edit the project file to target the framework version of your choice (the new style project format can be edited inside Visual Studio - just right click the project in the Solution Explorer and select "Edit project file"). For example:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>
</Project>

Further reading:

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
1

I found no answer to my satisfaction, so I figured out myself.

Here is the answer to you if you want to add external source codes to your project and don't want to copy over the entire codes. I have many dependencies on other gits and they are updated hourly if not minutely. I can't do copy every hour to sync up. Here is what you need to do.

Assume this is structure:

/root/projA/src

/root/projA/includes

/root/projB/src

/root/projB/includes

/root/yourProj/src

/root/yourProj/includes

  1. Start your VS solution.
  2. Right-click the project name right below the Solution.
  3. Then click the "Add", "New Filter", put the name "projA" for projA.
  4. Right-click on the "projA", click "Add", "New Filter", enter name "src"
  5. Right-click on the "projA", click "Add", "New Filter", enter name "includes"
  6. Right-click "projA"/"src", click "Add", "Existing Item", then browse to the /root/projA/src to add all source codes or one by one for the ones you want.
  7. Do same for "projA"/"includes"
  8. Do same for projB. Now the external/existing projects outside yours are present in your solution/project. The VS will compile them together. Here is an trick. Since the projA and projB are virtual folders under your project, the compiler may not find the projA/includes.

  9. If it doesn't find the projA/includes, then right click the project, select the "Properties".

  10. Navigate to "C/C++". Edit "Additional Include Directories", add your projA/include as such "../projA/includes", relative path.

One caveat, if there are duplicated include/header files, the "exclude from project" on the "Header file" doesn't really work. It's a bug in VS.

davidj
  • 331
  • 2
  • 5
0

As far as I can tell, the only way to do this in VS2010 is akin to the drag and drop method. Right click the solution to which you want to add a project. The application menu will have an add ... item. Opening that, you find that one of the options is to add an existing project to the solution.

In the dialog box that opens, navigate to the folder containing the project file for the solution and select it. VS will, as part of importing that project file, also import the entire directory and, I assume any subordinate directories which are part of that project.

As this does require an existing project file, it won't be impossible to import a directory tree until that tree has been converted to a project.