目录
介绍
我有一些自定义设置,我在我的所有项目中使用。每次我开始一个新项目时,尤其是测试项目时,重置它们都可能是一件非常痛苦的事情。解决方案是自定义用户*.props 文件。我只做C++项目,所以你的里程可能会有所不同。
运作方式
通过Visual Studio中的项目属性向导设置的所有设置都保存在项目文件夹的<project_name>.vcxproj文件中。
该文件将包含如下所示的部分,每个可能的构建配置对应一个部分:
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
此指令指示Visual Studio导入user.props 文件。
UserRootDir
是 C:\Users\<name>\AppData\Local\Microsoft\MSBuild\v4.0\
Platform
是 Win32 或 x64
因此,它要查找的文件(如果存在)是:
C:\Users\<名称>\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props
和/或
C:\Users\<名称>\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props
如果这些文件尚不存在,则可以创建它们。
我得到了这两个文件,它们是相同的。内容是:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import Project="G:\pja\Property Sheets\PJA.props" />
</ImportGroup>
</Project>
就我而言,他们所做的只是导入我自己的自定义PJA.props 文件,我已将其保存在自己的工作文件夹中。
其内容是:
<?xml version="1.0" encoding="utf-8"?>
<!-- PJA.props -->
<!-- contains my own custom project settings -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Add my include and lib folders to the default include and lib paths -->
<PropertyGroup>
<IncludePath>G:\pja\include;G:\Boost\boost_1_72_0;G:\elmax-0.9.5b\PortableElmax\PortableElmax;$(IncludePath)</IncludePath>
<LibraryPath>G:\pja\lib;G:\Boost\boost_1_72_0\stage\lib;G:\elmax-0.9.5b\PortableElmax;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<!-- Set the default C++ language version to C++20 and C language version to C17 -->
<ItemDefinitionGroup>
<ClCompile>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
</ItemDefinitionGroup>
</Project>
现在,每当我开始一个新项目时,它都会自动指定我自己的包含和库文件夹。我还将C和C++语言设置设置为使用C17和C++20。
现在,如果我需要更改或添加某些内容,我只需编辑自己的道具文件并使其正常工作即可。
若要了解需要输入哪些设置以及条目的正确语法,我将在Visual Studio属性向导中进行更改,然后在*.vcxproj 文件中搜索更改的设置。然后将该设置复制到我的PJA.props 文件中。
其他语言
我确信这种技术适用于Visual Studio中的其他语言。C#使用*.csproj 文件,您应该能够在其中执行相同的操作。我没有尝试过,因为我不使用任何其他语言。
兴趣点
弄清楚这一点很有趣。它涉及读取vcxproj文件。很多令人沮丧的谷歌搜索,大多数结果都说这是不可能的。如果犯了错误,Visual Studio会拒绝正常运行,并进行了大量的试验和错误。我为自己写了这个技巧,以提醒我是如何做到这一点的,也许其他人可能会觉得它有用。
https://www.codeproject.com/Tips/5380771/Saving-Visual-Studio-Settings-for-All-Your-Project