Use Project Linker to share source code in multiple projects

Page updated :

What is an extended add-in?

First, note the add-in before the step-by-step instructions.

This time I'm using an extension add-in called Project Linker 2012 in Visual Studio 2012, but there's also a project linker that has almost the same functionality. It's the same feature, but the difference between the producer and Project Linker only works fine in Visual Studio 2010. This time we're going to explain it in Visual Studio 2012, so the add-in we're going to use will be Project Linker 2012.

Conversely, if you're using Visual Studio 2010, you can use Project Linker. I think that it can be set in almost the same way as what is described in this time.

Sharing source code between multiple projects

The sharing of source code between multiple projects is standard in Visual Studio. Since the source file basically needs to be placed below the folder where the project file is located, for example, if you drag the source file of Project A to Project B, it will be a copy of the file and you will have double control of the code.

Let's start with the source code sharing feature in Visual Studio. To put it simply, it is an image that makes a link (shortcut) of the source file. To create a link to the code:

We are creating a project called Application1 and Application2, in which Application1 is the entity of the source file, and Application2 refers to the source file. In the image below, the "ProfileLoader.cs" file is in Application1, so make it referenced by the Application2 project.

Application1 が ProfileLoader.cs ファイルを持つ

Right-click the Application2 project and choose Add - Existing Item.

既存の項目を追加

In the file selection dialog box that appears, select the "ProfileLoader.cs" file in the folder of "Application1" from which it is referenced, click the "To the right of the Add button" button at the bottom right, and select "Add as Link".

ソースファイルをリンクとして追加

The "ProfileLoader.cs" file is added to the Application2 project. It is only a link to the ProfileLoader.cs file in Application1, not a copy. You can see that a square and arrow icon has been added to the bottom right of the file's icon.

If you open and edit the ProfileLoader.cs file for Application2 in this state, you are ProfileLoader.cs the application file for Application1.

Application2 にファイルのリンクが追加される

The referenced file, of course, is the same as when you put the source file normally. The namespace remains Application1, so you can use the class as usual by specifying the namespace as shown below.

参照したソースコードを使用

Use Project Linker to automate source code sharing

If you only have two projects sharing source files, or if you have only a few files to share, you can do it manually as described above, but if you have more projects or files, it can be quite a hassle manually. Since the number of files that can be shared at one time by the above operation is one folder unit, if you are layering folders, you will have to follow the same steps. Also, if you want to change the hierarchy of folders, you must re-configure them in all projects.

You can automate these operations with the following Visual Studio extension, Project Linker. Note that extensions cannot be used unless Visual Studio's Edition is Professional or higher.

Download from the following URL:

Project Linker 2012 ダウンロード

Once you have downloaded the file, double-click it to start it. It is in the form of an installer. Please close all Visual Studio before installing.

ProjectLinker.vsix ファイルをダブルクリックしてインストール

Make sure that Visual Studio is checked for installation and click the Install button.

インストール画面

Please close it when it is installed successfully.

インストール完了

When the installation is complete, open the solution file with multiple projects. Here, the project "Application1" has the entity of the source file, and "Application2" has its reference.

Right-click the project you want to reference and select Add project link.

コード構成を共有するプロジェクトのリンク作成

In the dialog that opens, select the project from which you want to refer to "Applicaion1". As the title says "Select Source Project", select the project that has the entity of the source file. When you're done, click the OK button.

If Link all items by default is checked, Application1's file and folder configuration will be synchronized to Application2 when you press the "OK" button, so if you don't want to synchronize them all of a sudden, clear them. On the other hand, if you want to synchronize in bulk, it is convenient to check.

コード参照元プロジェクトの選択

When the link is successful, click the OK button.

リンク成功

Application2 is linked to Application1, so let's add a class to Application1. After creating a link to the project, the operation is basically done in the way of "Application1" which is the reference source.

Right-click the project and choose Add - New Item.

追加 - 新しい項目

Add a class. Classes can add what they need. Here we are creating a class called StringHelper that assists in string editing.

クラスの追加

When you add a source file, a link to the file is automatically created in the Application2 project.

ソースファイルのリンクが自動的に作成される

Try to create a folder.

フォルダの作成

Folders are also automatically created. By the way, there is no concept of links in folders, so it is physical folders that are created.

フォルダも自動的に作成される

If you move the file into the folder you just created, it will be automatically moved to the destination as well.

ファイルの移動

Try to delete the folder and the files it contains.

フォルダの削除

The link is also deleted in the same way. In this way, you can use Project Linker to synchronize file and folder configurations between multiple projects.

フォルダとファイルの削除も同期される

When more than one project is linked, you can see what is currently in place. From the Project menu, select Edit project links.

Here you can check the list of links and unlink them. If you want to unlink, select the link from the list and click the Unlink button.