How to "Add Existing Item" an entire directory structure in Visual Studio?

Ian Patrick Hughes Source

I feel ridiculous for asking this because it seems like it should be so simple, however I have been unable to discover an answer to this question.

I have a free standing set of files not affiliated with any C# project at all that reside in a complicated nested directory structure. I want to add them in that format to a different directory in an ASP.NET web application I am working on; while retaining the same structure. So, I copied the folder into the target location of my project and I tried to “add existing item” only to lose the previous folder hierarchy. Usually I have re-created the directories by hand, copied across on a one-to-one basis, and then added existing items. There are simply too many directories/items in this case.

So how do you add existing directories and files in Visual Studio 2008?

visual-studioide

Answers

answered 10 years ago Darren Kopp #1

Drag the files / folders from Windows Explorer into the Solution Explorer. It will add them all. Note this doesn't work if Visual Studio is in Administrator Mode, because Windows Explorer is a User Mode process.

answered 10 years ago Brannon #2

Enable "Show All Files" for the specific project (you might need to hit "Refresh" to see them)**.

The folders/files that are not part of your project appear slightly "lighter" in the project tree.

Right click the folders/files you want to add and click "Include In Project". It will recursively add folders/files to the project.

** these buttons are located on the mini Solution Explorer toolbar.

** make sure you are NOT in debug mode

answered 7 years ago nader #3

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."

answered 6 years ago Don Rule #4

I didn't immediately understand this based upon these descriptions but here is what I finally stumbled on:

  1. Turn on "Show All Files" - there is an icon on the Solution Explorer toolbar
  2. Using Windows Explorer (not solution explorer), move your files into the directory structure where you want them to reside
  3. Click "Refresh" also on the Solution Explorer toolbar
  4. The files that you've moved should be visible "ghosted" in the Solution Explorer tree structure where you've placed them
  5. Right click on your ghosted files or folders and click "Include in Project". All the contents of a folder will be included

answered 6 years ago Tom #5

In Solution Explorer:

  1. Click Show All Files (Second icon from the left at the top of Solution Explorer)
  2. Locate the folder you want to add
  3. Right-click and select "Include in Project"

I use this to install add-ons like html editors and third-party file browsers.

answered 6 years ago user1395598 #6

In Windows 7 you could do the following:

Right click on your project and select "Add->Existing Item". In the dialog which appears, browse to the root of the directory you want to add. In the upper right corner you have a search box. Type *.cs or *.cpp, whatever the type of files you want to add. After the search finishes, select all files, click Add and wait for a while...

answered 4 years ago ILIA BROUDNO #7

I just want to point out that the both solutions offered above
- Drag and drop from Windows Explorer
- Show All files and then Include in Project.
Do not do what the Question asked for:
Include in project WHILE PRESERVING THE DIRECTORY STRUCTURE.
At least not in my case (C++/CLI Project Visual Studio 2013 on Windows 7).
In Visual Studio once you are back in the normal view (not Show All files) the files you added are all listed at the top level of the project.
Yes on disk they still reside where they were but in Solution Explorer they are loose.

I did not find any way around it except recreating the directory structure in Solution Explorer and then doing Add Existing Items at the right location.

answered 3 years ago Bjego #8

This is a little late but you can change your project XML to add existing subfolders and structures automatically into your project like "node_modules" from NPM:

This is for older MSBuild / Visual Studio versions

<ItemGroup>
   <Item Include="$([System.IO.Directory]::GetFiles(&quot;$(MSBuildProjectDirectory)\node_modules&quot;,&quot;*&quot;,SearchOption.AllDirectories))"></Item>
</ItemGroup>

For the current MSBuild / Visual Studio versions:

Just put it in the nodes of the xml:

<Project> 
</Project>

In this case just change $(MSBuildProjectDirectory)\node_modules to your folder name

answered 2 years ago lama12345 #9

It's annoying that Visual Studio doesn't support this natively, but CMake could generate the Visual Studio project as a work around.

Other than that, just use QT Creator. It can then export a VS project.

answered 2 years ago Marvin Glenn Lacuna #10

In addition to the answers. Below is the icon for the 'Show All Files' , just for easy reference.

enter image description here

answered 2 years ago Nick #11

What worked for me was to drag the folder into VS, then right click the folder and select "Open Folder in File Explorer". Then select all and drag them into the folder in VS.

answered 2 years ago Jason Kester #12

The cleanest way that I've found to do this is to create a new Class Library project in the target folder, and redirect all of its build output elsewhere. It still leaves a .csproj file sitting in that folder, but it does let you see it in Visual Studio and pick which files to include in your project.

answered 1 year ago Yatrix #13

A neat trick I discovered is that if you go to "Add existing...", you can drag the folder from the open dialog to your solution. I have my VS to open in Admin Mode automatically, so this was a good workaround for me as I didn't want to have to undo that just to get this to work.

answered 1 year ago Naren #14

At last, Visual Studio 2017 allows the user the import the entire directory with a single click. VS 2017 has a new functionality "Open Folder" that allows opening the entire folder, even without the need to save it as solution. The source code can be imported using the following methods.

  1. File -> Open -> Folder (ctrl + shift + o)
  2. devenv.exe < source folder>

It even supports building and debugging cmake projects.

https://blogs.msdn.microsoft.com/vcblog/2016/10/05/bring-your-c-codebase-to-visual-studio-with-open-folder/

answered 1 year ago Willy David Jr #15

This is what I do:

  1. Right click on solution -> Add -> Existing Website...
  2. Choose the folder where your website is. Just the root folder of the site.

Then everything will be added on your solution from folders to files, and files inside those folders.

answered 2 months ago Manoj Kalluri #16

enter image description here

click above in red circle, your folder will appear in solution explorer

right click on folder -> include in project.

comments powered by Disqus