Move file/class to another project

17,541

Solution 1

With resharper: Right Click (on file in Solution Explorer) -> Refactor -> Move.

Edit by jgauffin:

  1. Put the cursor over your non-existant class in your test method and press ALT-ENTER
  2. Choose to create the class in the context menu
  3. Press END to move to the class name
  4. Press CTRL+SHIFT+R to bring up the Resharper Refactor menu
  5. Press key DOWN for "Move to folder" and then press ENTER
  6. Press TAB in the new dialog and then ENTER to show the Target tree view (below the combo box)
  7. Navigate with the arrow keys
  8. Press ENTER when done

Solution 2

"Shift + Drag" in VS Solution Explorer allows you to move file(s). You can then use Resharper to fix the namespaces

Share:
17,541
jgauffin
Author by

jgauffin

coderr.io Automated exception handling for .NET - Coderr finds more of your errors and let you solve them faster. (I'm one of the founders). https://coderr.io | https://github.com/coderrio/ About me Blog | CV | Twitter My open source projects: Griffin.Framework Business application library Griffin.Yo A SPA library/framework written in TypeScript MarkdownWeb Web application for writing developer documentation

Updated on June 08, 2022

Comments

  • jgauffin
    jgauffin about 2 years

    Is it possible to move a class to another project with the help of Visual Studio or Resharper (with or without a plugin)?

    Would make TDD a tad easier.

    Update

    I use Resharper to create the class directly from the test method. The class is added in the same file as the test class. I can then ALT+Enter on the class to move it to a separate file. After that, I manually drag the file to the other project.

    But I would like to have a way to move it to another project directly without having to drag it. Something like ALT+ENTER on the non-existent class and choose "Create in Project Y"

    • Sayse
      Sayse about 11 years
      With visual studio you can just add existing item and then manually change the namespace (in the project you want to move to)
  • jgauffin
    jgauffin about 11 years
    And how do I move the class to a different project using Move?
  • Charleh
    Charleh about 11 years
    Usually just cut/paste the source file to the other project - then open the file and ALT+Enter the namespace and let ReSharper refactor it into the right place (based on the folder), not sure if there is a better way, but this is only a couple of clicks
  • jgauffin
    jgauffin about 11 years
    That's how I do it today, but imho it hurts the flow. That's why I'm asking. Would be better if there was a way to create the file in the corresponding namespace but another project.
  • Nikita B
    Nikita B about 11 years
    @jgauffin: Right Click on file containing the class in your solution explorer, not on the class declaration. You can specify destination from there (given you are moving the file between projects in the same solution)
  • jgauffin
    jgauffin about 11 years
    I can't find an option for another project there either. But if I'm in the solution explorer I could just drag the file to the correct project/namespace.
  • Nikita B
    Nikita B about 11 years
    @jgauffin: don't you see a "target folder" option? You can choose a project from there. And yes, obviously, you can move file manually, you'll have to fix internals and namespaces afterwards tho. While "Move" does all that in a single transaction and notifies you about missing assemblies
  • jgauffin
    jgauffin about 11 years
    sorry. My mistake. Was looking in the wrong place in the Move dialog. Too bad that it doesn't work with the keyboard :(
  • Nikita B
    Nikita B about 11 years
    @jgauffin: if by keyboard you mean hot keys - they work, when solution explorer is focused. You probably need to set those hotkeys first, tho
  • jgauffin
    jgauffin about 11 years
    No, I mean navigate the target treeview using the keyboard.
  • yoyo
    yoyo over 4 years
    Nice, works in Visual Studio with no extra plugins! On a large project it did cause my Visual Studio to hang for a minute or so, with CPU usage at 30%.