简体   繁体   中英

Adding common files to different projects in C#

I have 2 project in difference paths. I want to use some common files (.cs files) in both of projects.

D:\Tests\WindowsFormsApplication_1\WindowsFormsApplication_1
D:\Tests\WindowsFormsApplication_2\WindowsFormsApplication_2
D:\Tests\Common

How do I add this common files from one path ("Common" folder) for both of the projects?

Yes, You can. Right Click on your project, "Add" -> "Existing Item...", select your *.cs file and click on Down arrow next to the "Add" button and select "Add As Link".

There is no way to use same files in 2 different project without copying them. But you can add the third project in type of class library and put your shared files in it then add references to that project in your previous projects

If you want the common files to be available to edit in your other projects then follow user4015859's suggestion re setting up a class library project for your common files, but rather than adding a reference to that project in your other solutions, add the actual project to your solution by right clicking on the solution and select Add > Existing Project...

You still need to add a reference but in the reference Manager dialog (right click on references to open it) select Projects on the left hand side and your common project should be listed. Tick it and click OK.

To use the classes etc from the common project just add its namespace in the units where you want to use it.

This way you can edit the common files from either of your other solutions and you don't need to manually build your common solution when any of the files are changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM