当前位置:网站首页>C migration project record: modify namespace and folder name

C migration project record: modify namespace and folder name

2022-04-23 20:24:00 First code

C# To modify the file name and namespace of the project, it seems that Java IDE Modifying the package name is as simple as , Directly modifying the local folder and other operations to migrate the project will cause the solution to not open or namespace configuration and other errors after opening and running .

Recently, it is often necessary to migrate the project framework , Occasionally, you need to rename the project namespace , According to the online information and after successful operation , Record the following actions , For memory and learning :

To be modified C# Namespace and file name , Need experience Change solution name -> Project name -> Change namespace name -> Folder name several steps , The specific operation is as follows :
1)  Change solution name

This simple , open VS after , Directly on the solution , Right click - rename

2) Modify project name

This is also simple , Similar to the above , Just select the specific project name under the solution , Right click - Just rename . The key to this local problem is , We go through VS After modifying the project name, it is generally assumed that the name of the local folder will also change , There is no , It just changed VS The displayed item name , The corresponding folder name and namespace name are unchanged .( Therefore, the namespace and folder name will be modified later )

3) Modify namespace name

3-1 Modify code namespace name

Namespace name modification is critical , think .cs The code starts with a namespace index , If the namespace is incorrect , That can't run directly , This operation is also a bit confusing , Even if it's a trick .

Open one .cs Program files , Found namespace namespace, Change it to the name you want , After modification, a small red box with an error will appear in the namespace , Here's the picture

There is an error , Obviously not yet , Click on the... Box , There will be a selection that allows you to duplicate all namespaces under this project , You click on it , The namespaces of program files that already exist in all projects have been renamed , The namespace is automatically modified .

3-2 Modify the default namespace name in the project properties

3-1 although , Complete the modification of namespace , however C# It's still a pit ,( Not as good as java The same package name has been modified , It's all right ). We also need to change the default namespace name in the project properties , In this way, after creating a new program file, you can ensure that the namespace name is correct .

Right click the project name -> attribute , Change your name to what you want , And the previous namespace , It's better to have the same name as the folder , This is more convenient .(ctrl+s preservation )

4 Modify the name of the folder

The front by VS Complete namespace and other changes , however VS The folder name will not be changed automatically ( Generally, we will match the namespace name with the folder name ), We need to modify it manually .

close VS, Find the location where the item is stored on the computer , Click on the solution name .sln Solution file for , Configure the local folder name , If we want to change the folder name , This place should also be modified synchronously and manually , Otherwise, the program cannot be opened .

Solution name .sln, Right click Notepad to open ,

After the save , You can change the name of the local folder , Then reopen the project .

follow-up :

When it's done , The new project can basically run , I don't have a request to jump in, but I won't , In fact, you can't enter the specified back-end service , Printed through the browser console log It can be seen that it is 500 error , The error you see after clicking is as follows :

This error is that the corresponding request service cannot be found , As you can see from the above, there is conflict , Make the code overwhelmed , I don't know which one to use , The reason for this is that we changed the namespace , There are also some sequelae of file namespaces , For example .ashx Of xml The original namespace remains in :

The solution is , Make a complete replacement of the solution :

At the same time, the solution bin Under the folder , Delete the project package left by the previous namespace compilation :

 

reference :C# How to modify the project file name and namespace after the form project is established

版权声明
本文为[First code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210551268053.html