How to rename a Project Folder from within Visual

2019-01-01 13:49发布

My current solution for renaming the project folder is:

  • Remove the project from the solution.
  • Rename the folder outside Visual Studio.
  • Re-add the project to the solution.

Is there any better way?

30条回答
深知你不懂我心
2楼-- · 2019-01-01 14:40

Right Click project> Properties> & Set (Name) to the required name

查看更多
十年一品温如言
3楼-- · 2019-01-01 14:41

NOTE: This fix is for Visual Studio 2008, but should work here.

  1. Using Windows Explorer, rename both the solution folders (the parent folder and the child folder) to the new solution name.
  2. Delete the .sln file located in the parent folder.
  3. In Visual Studio, select File|Open Project.
  4. Drill into the new folder you just renamed and open the .csproj file (located in the child folder).
  5. Right-click the project name and rename it to what you want. (Should be the same name as the folder in step 1.)
  6. Select File|Close Solution. A dialog will ask if you want to save changes to the .sln file. Click Yes.
  7. In the Save File As dialog, point to the newly renamed PARENT folder and click save. (NOTE: Make sure the .sln file has the same name as the folder. Not required, but maintains consistency.)

DONE.

查看更多
浪荡孟婆
4楼-- · 2019-01-01 14:43

This is straight forward in VS 2015 (possibly works in older versions)

  1. In Solution Explorer, right click on Main solution > Rename
  2. In Solution Explorer, right click on project (under solution) > Rename
  3. In Solution Explorer, double click, or right click on Properties >goto Application Tab, rename Assembly name and Default namespace to match.
  4. If you wish to also rename the namespace, Open one of your class files. Right click the namespace > Rename... this should search and replace all references to your namespace throughout the project.
  5. Close the project > rename the project folder.
  6. Edit .sln file in notepad, change the path to the cjproj. ie fu\bar.csproj --> bar\bar.csproj
查看更多
步步皆殇っ
5楼-- · 2019-01-01 14:43

Man, have I struggled with this. Unfortunately there isn't a one click solution in Visual Studio, but if you're running Visual Studio 2012 and your project is under source control with Team Foundation Server, here is how I got it to work, while keeping the source history:

(Make sure you read @mjv's comment below, as he notes that you can skip step 5-10)

  1. Make sure you have checked in all changes, so you have no pending changes.
  2. Remove the project from the solution, by right clicking and selecting Remove.
  3. Now, in Windows Explorer, rename the project folder.
  4. Go back to Visual Studio, and in Solution Explorer, right click the solution and choose Add -> Existing project. Select the project file for the project you removed in step 2, which should be located in the renamed folder.
  5. Now the project is back in the solution, but the project doesn't seem to be added to source control. To fix that, open Source Control Explorer.
  6. Find the project folder in Source Control Explorer, that corresponds with the project folder on your disk, that you renamed in step 3.
  7. Rename the folder in Source Control Explorer, so it has the same name as the project folder on disk.
  8. Now take a look at your pending changes. You should have changes to the solution file and a rename operation on the project folder.
  9. Do a rebuild and make sure everything compiles correctly. If you had inter-project references to the project you renamed, you need to add them again to the individual projects that referenced it.
  10. You should be all set now. Go and check everything in.

The above guide worked for me. If it doesn't work for you, try and delete your local solution completely, and remove the folder mapping in your workspace. Restart Visual Studio just in case. Make sure you actually deleted the whole solution from your computer. Now readd the solution mapping to your workspace and get the latest version. Now try the above steps. The same applies if something goes wrong while following the above steps. Just delete your solution locally and get the latest source, and you'll have a clean slate to work with.

If you're still having problems, make sure that you haven't changed anything manually in the solution file, or trying other 'tricks' before trying the above steps. If you have changed something and checked it in, you might want to consider doing a rollback to the point just before you started messing with the renaming of the project.

Of course, you'd also want to rename the project itself, in Solution Explorer. You can do this before the steps above, but in that case, make sure you check in that change before applying the steps above. You can also do it afterwards, but make sure you follow all the steps above first, and check in your changes before trying to rename the project name in Solution Explorer. I don't recommend trying to mix the above steps with a rename of the project name in Solution Explorer. It might work though, but I would recommand doing it in 2 separate changesets.

查看更多
冷夜・残月
6楼-- · 2019-01-01 14:44

Currently, no. Well, actually you can click the broken project node and in the properties pane look for the property 'Path' then click the small browse icon and select the new path.

Voila :)

查看更多
栀子花@的思念
7楼-- · 2019-01-01 14:45

The simpler solution is the following:

  1. Right-click the project and rename it.
  2. (optional) Open the project’s property settings and modify the assembly name (and optionally the default namespace) to use the new project name.
  3. (optional) Select the namespace name in a source file, right click and select Refactor\Rename to globally rename the namespace to the new project name.
  4. (optional) Open the AssemblyInfo.cs file and change the assembly name to match.
  5. Save and close the solution.
  6. Using Windows Explorer rename the project folder to the new name.
  7. Open the SLN file in a text editor and find the one reference to the project path and change it to use the new folder name.

There are 4 needed steps but 7 recommended. At the end of the day though the project is renamed completely. Technically, the folder name for the project doesn’t have to match the project itself, so even that step is optional, but it can be confusing if they don’t match. Same for the assembly and namespace names.

查看更多
登录 后发表回答