visual-studio 在现有目录中创建项目/解决方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/923461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:33:45  来源:igfitidea点击:

Create Project/solution in an existing directory?

visual-studioidevisual-studio-2005visual-sourcesafe

提问by RBarryYoung

How can I create a New project & Solution in the same, existing , directory? No matter what I do, it keeps creating a new (sub)directory for the project and populating that directory instead of the one(s) that already exist. I have the "Create directory for Solution" checkbox cleared so that it will put the solution an dproject in the same folder and not create a solution folder, but it still keeps making the project sub-folder.

如何在相同的现有目录中创建新项目和解决方案?无论我做什么,它都会为项目创建一个新的(子)目录并填充该目录而不是已经存在的目录。我清除了“为解决方案创建目录”复选框,以便将解决方案和 dproject 放在同一文件夹中,而不是创建解决方案文件夹,但它仍然保持项目子文件夹。

How can I fix this?!?

我怎样才能解决这个问题?!?

This has bugged me for a long time, but I've been able to ignore it or work-around it up until now. But now it is kicking my butt productivity-wise on my customer project and I need to fix it. thanx... RBarryYoung

这已经困扰了我很长时间,但到目前为止我一直能够忽略它或解决它。但是现在它在我的客户项目上提高了我的生产力,我需要修复它。谢谢... RBarryYoung

(Note: This is VS2005, Team Edition for Software Developers)

(注:这是VS2005,软件开发者团队版)

采纳答案by heavyd

From Visual Studio, if you have no solution open, you can select

在 Visual Studio 中,如果您没有打开解决方案,您可以选择

File->New->Project From Existing Code

文件->新建->现有代码的项目

Select the project type

选择项目类型

Put the directory you want the solution and project file to be in as the "Project file location" field.

将您希望解决方案和项目文件所在的目录作为“项目文件位置”字段。

Finish the wizard and you'll have a project file and solution file in the same directory.

完成向导,您将在同一目录中拥有一个项目文件和解决方案文件。

If the "Project from existing code" option is not available

如果“从现有代码项目”选项不可用

Make a custom keyboard shortcut, toolbar button or menu item by using the customization features of VS.

使用 VS 的自定义功能制作自定义键盘快捷键、工具栏按钮或菜单项。

Tools->Customize

工具->自定义

Select the "Keyboard" button to add a keyboard shortcut or select the "Command" tab to add a toolbar button or menu item. The command you are looking for is:

选择“键盘”按钮添加键盘快捷键或选择“命令”选项卡添加工具栏按钮或菜单项。您正在寻找的命令是:

File->New Project From Existing Code

文件->从现有代码新建项目

Assign whatever keyboard shortcut or drag an icon to whichever menu/toolbar you want.

分配任何键盘快捷键或将图标拖动到您想要的任何菜单/工具栏。

回答by CashCow

To create a solution in an existing directory, I have found the best way is to create it one directory back from where you actually want it, then use the name of the directory where you really want it as the name of the solution.

要在现有目录中创建解决方案,我发现最好的方法是从您实际需要的位置创建一个目录,然后使用您真正需要的目录的名称作为解决方案的名称。

If that isn't the name you want (i.e. you don't want your solution name to match the name of the directory it is in) then once you have created it, you can rename the solution by right-clicking on it and choosing "Rename". It does not rename the directory it is in nor move it. That is by far the simplest way.

如果这不是您想要的名称(即您不希望您的解决方案名称与其所在目录的名称相匹配),那么一旦您创建了它,您可以通过右键单击它并选择来重命名该解决方案“改名”。它不会重命名它所在的目录,也不会移动它。这是迄今为止最简单的方法。

You can obviously use the same trick for a project too, however if you have more than one project in a single directory, ideally the intermediate directory into which they build should differ, otherwise you are going to have issues if you want to rebuild just one of them and it clears out the intermediate target directory.

您显然也可以对一个项目使用相同的技巧,但是如果您在一个目录中有多个项目,理想情况下它们构建的中间目录应该不同,否则如果您只想重建一个,则会遇到问题其中,它会清除中间目标目录。

回答by Mehmet Aras

You can always move things around manually and edit sln file to fix the project references. For example, if you want your solution file to be in the same directory as the project file, you can cut the solution file from wherever it is and paste it into the project file directory. Then you need to edit the solution file to fix the project reference so that the solution can find the project when it is opened.

您始终可以手动移动内容并编辑 sln 文件以修复项目引用。例如,如果您希望您的解决方案文件与项目文件在同一目录中,您可以从任何位置剪切解决方案文件并将其粘贴到项目文件目录中。然后你需要编辑解决方案文件来修复项目引用,这样解决方案在打开时就可以找到项目。

回答by Fredrik M?rk

I don't know of any out-of-the-box solutions for you. But, if the code files are organized into folders according to the wanted structure you could hack together a tool to do it for you, perhaps. The project files are not awfully complex in their structure, they are rather straight-forward XML documents, largely consisting of a list of the files that are included in the project.

我不知道您有什么现成的解决方案。但是,如果代码文件根据所需的结构组织到文件夹中,您也许可以组合一个工具来为您做这件事。项目文件的结构并不复杂,它们是相当简单的 XML 文档,主要由项目中包含的文件列表组成。

I would assume that the resulting project files may still require some manual labour, but you should be able to get the code to do the heavy work for you. And you may get a nifty tool to post on your blog.

我认为生成的项目文件可能仍需要一些体力劳动,但您应该能够让代码为您完成繁重的工作。你可能会得到一个漂亮的工具来张贴在你的博客上。

回答by julian bechtold

  1. Open an existing project. If you don't have one yet, create a temporary one.
  2. Select "File" > "new" > "Project"
  3. Make sure "Create new Solution" is selected
  4. Choose the type of project you want.
  5. Press Ok to create the project
  1. 打开一个现有的项目。如果您还没有,请创建一个临时的。
  2. 选择“文件”>“新建”>“项目”
  3. 确保选择“创建新解决方案”
  4. 选择您想要的项目类型。
  5. 按确定创建项目

If you created a temporary project in step one, you can delete it now

如果您在第一步中创建了一个临时项目,您现在可以将其删除

-> now you have created a project where the solution file is within the project enter image description here

-> 现在您已经创建了一个项目,其中解决方案文件位于项目中 在此处输入图片说明

回答by Abdullah Beyazta?

            public static class SlnFileGenerator
            {
                #region Public Methods
                public static string GetBatFileContent(string directoryPath, string slnFileName)
                {
                    if (directoryPath == null)
                    {
                        throw new ArgumentNullException(nameof(directoryPath));
                    }

                    if (slnFileName == null)
                    {
                        throw new ArgumentNullException(nameof(slnFileName));
                    }

                    var sb = new StringBuilder();
                    sb.AppendLine($"dotnet new sln --name {slnFileName}");

                    foreach (var csprojFile in Directory.GetFiles(directoryPath, "*.csproj", SearchOption.AllDirectories))
                    {
                        var relativePath = csprojFile.RemoveFromStart(directoryPath);

                        sb.AppendLine($"dotnet sln \"{slnFileName}.sln\" add \"{relativePath}\"");
                    }

                    sb.AppendLine("pause");

                    return sb.ToString();
                }
                #endregion
            }