如何正确编写批处理文件以供发布在.NET编程过程中添加不同的预编译宏?

问题描述 投票:0回答:1

我有以下代码:

#if PC
    var rootPath = Path.Combine("dist");
#else
    var rootPath = Path.Combine(@"/bin/bash");
#endif

我想在发布程序时编写不同的批处理文件来决定是否添加宏“PC”。

但是我在dotnetpublish命令下没有找到答案。

c# .net msbuild dotnet-publish
1个回答
0
投票
  1. 在 .csproj 文件中添加配置 (Pc),然后添加

    PC
    符号:

    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Pc|AnyCPU'">
        <DefineConstants>$(DefineConstants);PC</DefineConstants>
    </PropertyGroup>
    

    使用

    -c
    选项选择此配置:

    dotnet publish -c:Pc
    
  2. 使用 msbuild 的

    -getProperty
    -p
    选项来定义符号:

    SET cmdGetProp='dotnet msbuild -getProperty:DefineConstants'
    FOR /F "tokens=*" %%s IN (%cmdGetProp%) DO SET prop=%%s
    dotnet publish -p:DefineConstants=\"%prop%;PC\"
    
© www.soinside.com 2019 - 2024. All rights reserved.