我如何可以将现有的文件夹结构添加到我的Visual Studio项目(Python的工具)?(How

2019-08-01 01:50发布

This question is essentially the same as these:

How do I add an existing directory tree to a project in Visual Studio? How to "Add Existing Item" an entire directory structure in Visual Studio?

Except the solutions don't work for me.

It looks like another user had the exact same problem

http://pytools.codeplex.com/discussions/249455

But http://xkcd.com/979/ has struck again.

I am using Visual Studio 2010 with Python Tools for Visual Studio.

In this project users create new folders and code and commit them to SVN. Another user will update SVN and the new files and folders will show up in windows explorer. The user then needs an easy way to add these folders and files to the solution.

Putting the solution in SVN so added folders can be added to the solution before committing is not an option. The solution is controlled in a separate area from the source.

Suggested Solution:

https://stackoverflow.com/a/392477/606660

Won't work because:

The "Show all files" button in the solution explorer does not show up.

Suggested Solution:

https://stackoverflow.com/a/57778/606660

Won't work because:

When I drag the folder onto the solution explorer pane the location the folder is dropped affects where in the solution it is nested. If it gets dropped in the wrong folder, it shows up as a folder with the expected name and the expected contents. This is really close to what we want, except its in the wrong folder (since I dropped it on the wrong folder, intentionally). If the folder is dragged to the correct location, it appears as a file with an exclamation point. When you double click the "file" it says

"The item <folder name> does not exist in the project directory.  It may have been moved, renamed, or deleted"

I believe this is because VS will try to create a copy of the folder in the directory that you drag it to. If I move the folder out of my project entirely (say to the desktop) and then drag it into the solution explorer at the proper location, it shows up as a folder in the project. A copy of the folder is also created on disk at the location specified by the drop, with the same name and contents.

So it appears that dragging and dropping a folder onto the solution explorer will create a copy of the folder on the disk in the location that you targeted in your solution when you dropped it. If that location already has a folder of that name, the folder gets imported as a file.

My Solution

I'm using PyCharm instead, its much better.

Answer 1:

如果没有别的工作,你可以在手动添加文件和文件夹.pyproj -file。 格式很简单:

<ItemGroup>
    <Compile Include="File1.py" /> <!-- List of files relative to .pyproj folder -->
    <Compile Include="test\file2.py" />
</ItemGroup>
<ItemGroup>
    <Folder Include="test\" /> <!-- List of folders -->
</ItemGroup>

您可以添加更多的<ItemGroup>元素,如果你想,如果你愿意,你可以混合使用的文件和文件夹。

脚本生成的XML:

import os

def visit(folder):
    for fn in os.listdir(folder):
        filename = os.path.join(folder, fn)
        _, ext = os.path.splitext(fn)
        if os.isdir(filename):
            folders.append(filename)
            visit(filename)
        elif ext.lower() == '.py':
            files.append(filename)

files = []
folders = []

visit('.')

print '<ItemGroup>'
for fn in files:
    print '  <Compile Include="' + fn + '"/>'
print '</ItemGroup>'

if folders:
    print '<ItemGroup>'
    for fn in folders:
        print '  <Folder Include="' + fn + '\\"/>'
    print '</ItemGroup>'


Answer 2:

显示所有文件的作品。 显示所有文件在项目菜单。

项目 - >显示所有文件



Answer 3:

这在Visual Studio 2013为我工作,我不知道这是否适用于其他Visual Studio迭代。

安装完成后Python的工具为Visual Studio(PTVS),我发现在这里

  1. 打开Visual Studio中,导航到文件 - >新建>项目(或保持CNTRL +移+ N)

  2. 在新建项目对话框中,浏览到您现有的Python项目目录。 输入项目名称字段中输入项目的名称。 取消创建解决方案目录(如果你不希望创建一个新项目目录)。 并选择“从现有Python代码”选项,然后单击确定。

  3. 在“从现有的Python代码新建项目”向导,按照提示进行操作,使所有必要的设置根据需要或只需单击完成。



文章来源: How can I add an existing folder structure to my visual studio project (python tools)?