Search code examples
visual-studio-2019solution-folders

Move Project To a New Folder in Visual Studio


Here is my Solution files structure (folders are prefixed by +):

+FooProject
  FooProject.sln
  +Tests
    +BarTests
      BarTests.csproj

Now I want to move the BarTests project to a new subfolder UnitTests:

+FooProject
  FooProject.sln
  +Tests
    +UnitTests
      +BarTests
        BarTests.csproj

I tried the following ways but to no avail:

  1. If I add a New Solution Folder, UnitTests, in Visual Studio, and move the BarTests to UnitTests folder in Visual Studio, then no new folder is created on disk, and no changes to the solution file are made, hence can't propagate this change in the source control.
  2. I create the UnitTests folder directly on disk (using File Explorer or Command Line), move the BarTests to UnitTests, and then manually update the solution file to reflect this change in the path. Now the problem is in Visual Studio, I still see the BarTests project under the 'Tests' folder and not the UnitTests folder.

I tried the second approach even after closing VS and deleting the .vs folder but still the same behavior.

What am I missing here?


Solution

  • Solution folders within Visual Studio are just used to organise solution-level assets such as projects and other solution folders within Visual Studio itself and are not permeated to disk.

    When you move a folder on disk and change the reference to it within the .sln file, you are doing exactly that.

    Really simple solution, do exactly what you have tried already and you can even do it within Visual Studio by switching to "Folder View" within the Solution Explorer.

    1. Unload or remove the BarTests project from the solution within Visual Studio.
    2. Create your physical "UnitTests" target folder on disk in the desired location and move the BarTests project folder and files to the target location.
    3. Return to Visual Studio and repeat, create your solution folder for UnitTests and then add your project to that solution folder (if you removed then you select "Add existing project" from the menu).