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

问题描述:

此问题与以下内容基本相同:

This question is essentially the same as these:

如何如何在Visual Studio中将现有目录树添加到项目中? 如何添加现有项目"整个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

但是 http://xkcd.com/979/又来了.

我正在将Visual Studio 2010与适用于Visual Studio的Python工具一起使用.

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

在此项目中,用户创建新文件夹和代码并将其提交到SVN.另一个用户将更新SVN,新文件和文件夹将显示在Windows资源管理器中.然后,用户需要一种简单的方法来将这些文件夹和文件添加到解决方案中.

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.

将解决方案放入SVN中,因此不能在提交之前将添加的文件夹添加到解决方案中.该解决方案在与源不同的区域中进行控制.

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.

建议的解决方案:

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

无法正常工作,因为:

解决方案资源管理器中的显示所有文件"按钮未显示.

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

建议的解决方案:

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

无法正常工作,因为:

当我将文件夹拖到解决方案资源管理器窗格中时,放置文件夹的位置会影响该文件夹在解决方案中的嵌套位置.如果将其放置在错误的文件夹中,它将显示为具有预期名称和预期内容的文件夹.这真的很接近我们想要的东西,只是它放在错误的文件夹中(因为我有意将它放到了错误的文件夹中).如果将文件夹拖到正确的位置,它将显示为带有感叹号的文件.当您双击文件"时,它会显示

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"

我相信这是因为VS会尝试在您将文件夹拖动到的目录中创建该文件夹的副本.如果我将文件夹完全移出项目(例如移至桌面),然后将其拖到解决方案资源管理器中的适当位置,它将在项目中显示为文件夹.文件夹的副本也会在磁盘上由放置指定的位置创建,并具有相同的名称和内容.

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.

我的解决方案

我改用PyCharm,效果更好.

I'm using PyCharm instead, its much better.

如果没有其他效果,则可以在.pyproj文件中手动添加文件和文件夹.格式很简单:

If nothing else works, you could add the files and folders manually in the .pyproj-file. The format is simple:

<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>元素,并且可以根据需要混合文件和文件夹.

You can add more <ItemGroup> elements if you want, and you can mix files and folder if you wish.

生成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>'