0

目前我的项目有一个许可证文件。没有附加到文件的扩展名。该文件本身仅包含一个密钥。

我有 3 个构建配置

Dev
Stage
Prod

目前我有 4 个许可证文件。

GLicense 

GLicense_dev
GLicense_stage
GLicense_prod

我尝试使用 #c 预处理器指令,但第三方 dll 要求我的许可证名称与GLicense. 我想采取的下一个方法是在构建时覆盖 GLicense 的内容。我想知道我该怎么做?

4

2 回答 2

2

我相信真正的答案是在“复制到”中使用$(TargetDir)vs。$(ProjectDir)$(OutDir)您不想$(ProjectDir)在右侧提及。如果您的输出在您的项目文件夹之外怎么办,甚至可能是不同的计算机。另外,您可以打开属性并在“构建事件”中添加此单个事件,而不是更改项目 XML 中的项目文件

if $(ConfigurationName) == dev copy /y "$(ProjectDir)GLicense_dev" "$(TargetDir)GLicense"
if $(ConfigurationName) == stage copy /y "$(ProjectDir)GLicense_stage" "$(TargetDir)GLicense"
if $(ConfigurationName) == prod copy /y "$(ProjectDir)GLicense_prod" "$(TargetDir)GLicense"

另一种方式(这是真正的 Visual Studio 解决方案,因为它消除了 CMD/批处理编码。在您的情况下,您在 3 个不同的文件夹中有 3 个具有相同名称的不同文件),将您的 3 个文件作为 3 中的内容添加到项目中不同的文件夹,但文件名 - 相同 ( GLicense)。并在构建操作中选择“始终复制”。然后在项目文件 XML 中添加Condition=" '$(Configuration)' == 'dev'文件本身而不是属性组

<None Include="dev\GLicense" Condition=" '$(Configuration)' == 'dev'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="stage\GLicense" Condition=" '$(Configuration)' == 'stage'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="prod\GLicense" Condition=" '$(Configuration)' == 'prod'>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
于 2020-04-01T03:24:37.457 回答
1

修改您的项目预构建命令行以包含每个配置所需的源文件

copy /y "$(ProjectDir)GLicense_dev" "$(ProjectDir)$(OutDir)GLicense"

copy /y "$(ProjectDir)GLicense_stage" "$(ProjectDir)$(OutDir)GLicense"

copy /y "$(ProjectDir)GLicense_prod" "$(ProjectDir)$(OutDir)GLicense"

您的项目文件将如下所示

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'dev|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_dev.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'stage|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_stage.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'prod|AnyCPU' ">
  <PreBuildEvent>copy /y "$(ProjectDir)GLicense_prod.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
</PropertyGroup>
于 2020-03-31T19:30:28.067 回答