共用或重複使用 Visual Studio 專案設定Share or reuse Visual Studio project settings

若要在多個專案中建立自訂群組,您可以與其他人共用,或重複使用的設定,請使用屬性管理員來建立內容工作表頁(.props 檔案) 來儲存每個設定的您想要能夠重複使用或與他人共用的專案。To create a custom group of settings that you can share with others or reuse in multiple projects, use Property Manager to create a property sheet (.props file) to store the settings for each kind of project that you want to be able to reuse or share with others. 使用屬性工作表具有最少出錯比其他方式建立 「 全域 」 設定。Using property sheets are far less error-prone than other ways of creating "global" settings.

重要

.user 檔案,以及其問題的原因.user files and why they are problematic

Visual Studio 的過去版本會使用具有 .user 副檔名且位於 <使用者設定檔>\AppData\Local\Microsoft\MSBuild\v4.0\ 資料夾的全域屬性工作表中。Past versions of Visual Studio used global property sheets that had a .user file name extension and were located in the <userprofile>\AppData\Local\Microsoft\MSBuild\v4.0\ folder. 我們已不建議使用,因為這些檔案是按照每位使用者、每台電腦基準來設定專案組態的屬性。We no longer recommend these files because they set properties for project configurations on a per-user, per-computer basis. 這類「全域」設定可能干擾組建,尤其是在您以組建電腦上的多個平台為目標時。Such "global" settings can interfere with builds, especially when you are targeting more than one platform on your build computer. 例如,如果您同時有 MFC 專案和 Windows Phone 專案,那麼 .user 屬性對其中一個專案會是無效的。For example, if you have both an MFC project and Windows Phone project, the .user properties would be invalid for one of them. 可重複使用的屬性工作表具有較大彈性且更為穩固。Reusable property sheets are more flexible and more robust.

雖然 .user 檔案仍舊由 Visual Studio 安裝並且參與屬性繼承,但預設為空白。Although .user files are still installed by Visual Studio and participate in property inheritance, they are empty by default. 最佳做法是在 [屬性管理員] 中刪除其參考,以確保您的專案獨立運作,不受每個使用者、每部電腦設定的影響。這點對確保在 SCC (原始程式碼控制) 環境中產生正確行為非常重要。The best practice is to delete the reference to them in Property Manager to ensure that your projects operate independently of any per-user, per-computer settings This is important to ensure correct behavior in a SCC (source code control) environment.

若要顯示 [屬性管理員],請在功能表列上,選擇 [檢視]、[其他視窗]、[屬性管理員]。To display Property Manager, on the menu bar, choose View, Other Windows, Property Manager.

如果您有一組要套用至多個專案的常用共同屬性,您可以使用 [屬性管理員] ,將這些屬性擷取到可重複使用的「屬性工作表」檔案,其依慣例具有 .props 副檔名。If you have a common, frequently used set of properties that you want to apply to multiple projects, you can use Property Manager to capture them in a reusable property sheet file, which by convention has a .props file name extension. 您可以將一個或多個工作表套用至新的專案,這樣就不需要從頭設定其屬性。You can apply the sheet (or sheets) to new projects so that you don't have to set its properties from scratch. 若要存取 [屬性管理員],請在功能表列上,選擇 [檢視]、[屬性管理員]。To access Property Manager, on the menu bar, choose View, Property Manager.

屬性管理員捷徑功能表Property Manager shortcut menu

在每個組態節點下,您會看到該組態所套用的每個屬性工作表的節點。Under each configuration node, you see nodes for each property sheet that applies to that configuration. 系統會新增屬性工作表,這些屬性工作表是在您建立專案時,根據應用程式精靈中選擇的選項設定值。The system adds property sheets that set values based on options you choose in the app wizard when you create the project. 請以滑鼠右鍵按一下任何節點,然後選擇 [屬性],以查看套用到該節點的屬性。Right-click any node and choose Properties to see the properties that apply to that node. 所有屬性工作表都會自動匯入專案的「主要」屬性工作表 (ms.cpp.props),並依 [屬性管理員] 中的出現順序進行評估。All the property sheets are imported automatically into the project's "master" property sheet (ms.cpp.props) and are evaluated in the order they appear in Property Manager. 您可以移動它們以變更評估順序。You can move them to change the evaluation order. 稍後評估的屬性工作表將會覆寫先前評估之工作表中的值。Property sheets that are evaluated later will override the values in previously-evaluated sheets. 請參閱專案屬性繼承如需有關在.vcxproj 檔案、.props 和.targets 檔案、 環境變數和命令列中的評估順序。See Project property inheritance for more information about the order of evaluation in the .vcxproj file, the .props and .targets files, environment variables and the command line.

如果您選擇 [新增專案屬性工作表],然後選擇 (例如) MyProps.props 屬性工作表,就會出現屬性頁對話方塊。If you choose Add New Project Property Sheet and then select, for example, the MyProps.props property sheet, a property page dialog box appears. 請注意,這會套用至 MyProps 屬性工作表。您所做的任何變更都將寫入至工作表,而非專案檔 (.vcxproj)。Notice that it applies to the MyProps property sheet; any changes you make are written to the sheet, not to the project file (.vcxproj).

如果直接在 .vcxproj 檔案中設定相同屬性,就會覆寫屬性工作表中的屬性。Properties in a property sheet are overridden if the same property is set directly in the .vcxproj file.

您可以視需要匯入屬性工作表。You can import a property sheet as often as required. 方案中的多個專案可以繼承同一個屬性工作表中的設定,而且專案可以有多個工作表。Multiple projects in a solution can inherit settings from the same property sheet, and a project can have multiple sheets. 屬性工作表本身可以繼承另一個屬性工作表中的設定。A property sheet itself can inherit settings from another property sheet.

您也可以為多個組態建立一個屬性工作表。You can also create one property sheet for multiple configurations. 若要這麼做,請建立每個組態的屬性工作表,開啟其中一個工作表的捷徑功能表,選擇 [新增現有屬性工作表],然後新增其他工作表。To do this, create a property sheet for each configuration, open the shortcut menu for one of them, choose Add Existing Property Sheet, and then add the other sheets. 不過,如果您使用一個通用屬性工作表,請注意當您設定屬性時,這個屬性是針對工作表所套用至的所有組態來設定;還要知道 IDE 不會告訴您哪些專案或其他屬性工作表是繼承自指定的屬性工作表。However, if you use one common property sheet, be aware that when you set a property, it gets set for all configurations that the sheet applies to, and that the IDE doesn’t show which projects or other property sheets are inheriting from a given property sheet.

在具有許多個專案的大型方案中,建立方案層級的屬性工作表會很有用。In large solutions that will have many projects, it can be useful to create a property sheet at the solution level. 當您將專案新增至方案時,請使用 [屬性管理員] 將該屬性工作表新增至專案。When you add a project to the solution, use Property Manager to add that property sheet to the project. 如果這必須在專案層級進行,您可以加入新的屬性工作表來設定專案特定值。If required at the project level, you can add a new property sheet to set project-specific values.

重要

由於 .props 檔案不是建立做為專案項目,因此預設不參與原始檔控制。A .props file by default does not participate in source control because it isn’t created as a project item. 如果要在原始檔控制中包含這個檔案,可以手動將其加入做為方案項目。You can manually add the file as a solution item if you want to include it in source control.

建立屬性工作表To create a property sheet

  1. 在功能表列上,選擇 [檢視]、[屬性管理員]。On the menu bar, choose View, Property Manager. [屬性管理員] 隨即開啟。The Property Manager opens.

  2. 若要定義屬性工作表的範圍,請選取套用的項目。To define the scope of the property sheet, select the item to which it applies. 這可以是特定組態,或其他屬性工作表。This can be a particular configuration, or another property sheet. 開啟這個項目的捷徑功能表,然後選擇 [新增專案屬性工作表]。Open the shortcut menu for this item and then choose Add New Project Property Sheet. 指定名稱和位置。Specify a name and location.

  3. 在 [屬性管理員] 中,開啟新的屬性工作表,然後設定您要包含的屬性。In Property Manager, open the new property sheet and then set the properties you want to include.