How to rename assemblies and project folders

Locked How to rename assemblies and project folders

  • Friday, July 06, 2007 9:39 PM
     
     
    Hi all, I've cobbled some stuff together as a helper reference for renaming assemblies, folders and what not.  I found it hard to find all of this in one place, so I bit the bullet and "just did it". Lemme know if something isn't quite right.

    .........................................................................
    Renaming .NET projects and assemblies (oh my)

    Before starting, do realize that if you have any applications or consoles open that are accessing the project files or folders you will not be able to follow these steps to rename certain items. If you are running ildasm, or if you have a console open to do builds, or whatever, close them before beginning. Although this doc does not specifically address ASP projects, a browser that is accessing files in the project folder is going to stop you from following these steps.

    The big gotcha
    Unlike Java, a .NET class name and source file name could be the same, but they might be different. The ability to name the class and its source file differently is either called "flexibility" or "rope", as in enough rope to hang yourself, depending on which side of the tracks you're on. The same applies to projects and assemblies. A .NET project name, project folder name, and assembly name might be all the same, or they all might be different. Let's see, how many combinations is that?

    The question is: How do you change all of these names to be consistent with what you want after you realize that you misnamed them originally?  This is what refactoring sometimes does (or attempts to do) in certain IDEs.

    First of all there is refactoring in the VS2005 IDE, but it only applies to highlighted elements in the source code, not names like the project name, source file names, or more problematically - the project folder name. Changing those requires something more endowed than refactoring.

    Secondly, the Solution Explorer does allow you to rename source files. It doesn't allow you to rename folders though, so what it can do and can't do is usually confusing to beginners. Worse, it can be misleading to see that you can change a source file name, but can't change a project folder name. Remember the Maine farmer who gave the directions to the tourist, "You can't get there from here." Solution Explorer will also not allow you to rename your binary assemblies.  So, what happens if you manage to figure out how to change the physical project folder name but don't like having it not match the assembly name?  BOING! Ugh! Arggh!

    Below are all of the scenarios for renaming that you should require, unless you find one that I never needed or considered.

    1. Rename Project

    Renaming a project is easy. To rename the foo project to bar project:

     a. In Solution Explorer highlight the foo project | right click | rename foo to bar.

      Effect1: File foo.csproj becomes renamed to bar.csproj.

      Effect2: The foo.sln file still refers to "foo" instead of "bar".

     b. Rebuild solution and you're good to go.

    This does not rename the assembly even if the assembly name was the same as the project name. The assembly bin\Debug\foo.exe is still foo.exe even if you rebuilt the solution.  The project directory name is also still unchanged. Onward.....




    2. Rename assembly


     a. In Solution Explorer: right click the project folder | Properties | change the assembly name to bar | Save.

      Effects: Creates bar.vshost.exe but leaves the assembly set to foo.exe.

      Optionally fix the Assembly metadata to be consistent with the changes.  Click Assembly button and change the data.


     b. Push changes to the assembly: Rebuild the solution

      Effects: Now bin\Debug\foo.exe is bar.exe and bar.pdb.


    See how easy?  But your project physical directory is still not what you want?




    3. Rename the physical project directory

    Note: The physical path property is recorded in the .sln file so you cannot just rename the folder in Explorer.

     a. Close the solution and the IDE

     b. In Explorer: Change the directory name to the new name.

     c. In Explorer:  Open the .sln file with a text editor.

     c. Change the directory name to the new name and save.

     d. Restart the IDE and open the solution from the File, Recent Files menu if it doesn't start automatically.

     e. Click on the Project folder of the Solution Explorer and check the path property in the properties at the bottom. It will now be referencing to the new project folder.


    Now for the easy stuff.



    4. Rename a class source file

    A public class name doesn't have to match the source file name as in Java.  Therefore if you want to change either of these two entities, it's good to know what you're dealing with.


     a. In Solution Explorer: Right-click the source file.
     b. Rename (see response to message box below).
     c. Answer yes if you want your class name and references throughout the code to be consistent with the new file name.  It's optional to have the class name and source file name match.

    Effects: The source file name is changed and the class name references in the source are consistent with that.  



    5. Rename namespace

     a. Highlight the namespace name in the IDE.  This causes the Refactor menu to appear.
     b. Refactor, Rename.



    6. Rename default namespace

     a. Right-click the project in Solution Explorer
     b. Properties | Application | Default namespace



    7. Rename Solution physical directory

    This is a little trickier than the last two items, but not terribly so.

     a. Quit the IDE and close all apps or consoles that are using the solution directory or sub-directories.
     b. Rename the directory.
     c. Restart the IDE (it will warn you that it cannot find the last loaded solution, which is irrelevant now.)
     d. File, Open Project/Solution, pick the solution file in the new location.


    This does not change the Solution name, just the folder name.



    8. Rename the solution itself

     a. In Solution Explorer: Right-click the Solution and Rename.


    There - now you can rename your entire world.  

    Send comments or suggestions to dotpaul@paulNOSPAMleblanc.net by removing NOSPAM from the address.



All Replies

  • Friday, September 05, 2008 2:50 AM
     
     
    Thanks dude, the longer I don't work with Java at work the more I realise how good it is, even if I do spend most of my time swearing at the compiler.
  • Tuesday, April 24, 2012 1:52 PM
     
     

    Good write-up.

    Now all we need is the scenarios when you factor in Visual Sourcesafe and how to rename stuff in there. After it has been checked out and you make your changes whilst checked out you will discover a lot of conflicts when you try to check back in - (especially with folders that you have renamed).

    It's a mish-mash of bits that work and bits that you never want to try rename ever again.