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:32

For those using Visual Studio + git and wanting to keep file history (works renaming both projects and/or solutions):

1) Close Visual Studio

2) In .gitignore file duplicate all ignore paths of project you want to rename with renamed versions of those paths.

3) Use git move command like this:

git mv <old_folder_name> <new_folder_name>

See documentation for additional options: https://git-scm.com/docs/git-mv

4) In your .sln file: Find the line defining your project and change the folder name in path. The line should look something like:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "<Project name>", "<path-to-project>\<project>.csproj"

5) Open Visual Studio, right click on project -> Rename

6) Afterwards rename the namespaces. I read that resharper has some options for this. But simple find/replace did the job for me.

7) Remove old .gitignore paths

查看更多
怪性笑人.
3楼-- · 2019-01-01 14:32

Or simply,

Copy all the codes then open a new project with the desired name and paste the code. Run debug and then delete the previous project. Done!

It worked for me!

查看更多
残风、尘缘若梦
4楼-- · 2019-01-01 14:33

Although this question has already been answered I wanted to share my approach to solving this problem. I often had the same problem of renaming a project in VS and editing the folder name, project name and .sln file in order to accomplish that. I just wrote a VBScript that accomplishes all that. You have to be careful with the strings you choose for replacing.

EDIT: You just have to put the .vbs file in the same directory as the .sln file of the solution.

'Script parameters'
Solution = "Rename_Visual_Studio_Project" '.sln'
Project = "Rename_Visual_Studio_Project" '.csproj'
NewProject = "SUCCESS"

Const ForReading = 1
Const ForWriting = 2

Set objFso = CreateObject("Scripting.FileSystemObject")
scriptDirr = objFso.GetParentFolderName(wscript.ScriptFullName)

'Rename the all project references in the .sln file'
Set objFile = objFso.OpenTextFile(scriptDirr + "\" + Solution + ".sln", ForReading)
fileText = objFile.ReadAll
newFileText = Replace(fileText, Project, NewProject)
Set objFile = objFSO.OpenTextFile(scriptDirr + "\" + Solution + ".sln", ForWriting)
objFile.WriteLine(newFileText)
objFile.Close

'Rename the .csproj file'
objFso.MoveFile scriptDirr + "\" + Project + "\" + Project + ".csproj", scriptDirr + "\" + Project + "\" + NewProject + ".csproj"

'Rename the folder of the .csproj file'
objFso.MoveFolder scriptDirr + "\" + Project, scriptDirr + "\" + NewProject
查看更多
刘海飞了
5楼-- · 2019-01-01 14:34

In andersjanmyr's answer its easier to rename the project first.

  1. Rename project.
  2. Close the solution (save it).
  3. Rename the folders outside Visual Studio.
  4. Open the solution, ignoring the warnings.
  5. Go through all unavailable projects and set the property 'File Path' to the new location of your project file, i.e. someproject.csproj.
  6. Reload the project.

Also, after those steps are carried out you might want to rename other references to your old project name.

In project properties, update the Assembly Name and Default Namespace. This will update the following in the project file...

<RootNamespace>SomeProjectName</RootNamespace>
<AssemblyName>SomeProjectName</AssemblyName>

...and will get rid of the error "Namespace does not correspond to file location, should be: 'SomeProjectName'"

Rename your root namespace (if you have resharper right click the Namespace and go Refactor -> Rename).

Change all occurences of your old project name in AssemblyInfo.cs

查看更多
看风景的人
6楼-- · 2019-01-01 14:34

Well I did it my way

  • Close Visual Studio 2012
  • Rename your subdirectory to prefered name under .sln
  • Delete *.suo file
  • Open solution again, fix any properties of Project(s) loaded to meet new subdirectory name
查看更多
不再属于我。
7楼-- · 2019-01-01 14:34

What worked for me in 2017:

  • Close solution VS
  • Renamed directory of projects in solution.
    • (push change in source control - git in my case)
  • edit sln file in text editor (outside VS2017) changing name of directory.
  • Reopen solution in VS

It said something like "re-adding project", I rebuilt everything and everything was good to go.

查看更多
登录 后发表回答