How do I add an existing directory tree to a proje

2019-01-03 07:17发布

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?

10条回答
成全新的幸福
2楼-- · 2019-01-03 07:49

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.

查看更多
Viruses.
3楼-- · 2019-01-03 07:50

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>
查看更多
相关推荐>>
4楼-- · 2019-01-03 07:55

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

查看更多
我只想做你的唯一
5楼-- · 2019-01-03 07:57

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.

查看更多
We Are One
6楼-- · 2019-01-03 08:02

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

If you wanted 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" >

Reference: http://jamesrpatterson.com/blog/automatic-include-in-project-for-visual-studio

查看更多
太酷不给撩
7楼-- · 2019-01-03 08:03

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

查看更多
登录 后发表回答