You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET发布时如何编写批处理文件添加不同预编译宏?

通过批处理控制dotnet publish的预编译宏

你可以通过dotnet publish命令的/p:DefineConstants参数指定预编译宏,直接在批处理文件中编写对应命令即可实现需求:

1. 带PC预编译宏的批处理(publish-pc.bat)

dotnet publish -c Release /p:DefineConstants=PC

运行该脚本时,编译会启用#if PC分支的代码,最终rootPath会被设置为"dist"。

2. 不带PC预编译宏的批处理(publish-linux.bat)

dotnet publish -c Release

如果需要明确清空自定义预编译符号(避免继承项目中已有的其他符号),也可以写成:

dotnet publish -c Release /p:DefineConstants=

运行该脚本时,会走#else分支,rootPath会被设置为"/bin/bash"。

注意事项

  • 如果项目本身已定义其他预编译符号(比如RELEASE、TRACE),需要用分号分隔多个符号,防止覆盖默认配置:
    dotnet publish -c Release /p:DefineConstants=PC;RELEASE;TRACE
    
  • 发布后可在代码中添加控制台输出或日志,验证rootPath的值是否符合预期。

内容的提问来源于stack exchange,提问作者MM丶

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:20:54