【发布时间】:2022-01-02 12:33:14
【问题描述】:
如果我转到 Project->MyApp->Properties->Settings 并输入 Name like WhichApp of type string 并输入 Scope 作为 PCB 的 Application 和 Value。当我在
我的主窗体使用MessageBox.Show(Properties.Settings.Default.WhichApp); 我看到一个显示“PCB”的消息框。
但如果我在查看属性值时尝试使用 MSBuild.exe -property:WhichApp=SUB 设置属性,我仍然会看到“PCB”。
如何在构建时调用 MSBuild.exe 编译工具设置属性?
我尝试在 Visual Studio 中使用外部工具..
标题:设置属性
命令:C:\Program Files(x86)\Microsoft Visual Studio\2017\WDExpress\MSBuild\15.0\Bin\MSBuild.exe
参数:-property:WhichApp=SUB
初始目录:$(ProjectDir)
----我试图在主窗体的 LOAD() 方法中完成的内容----
if (Properties.Settings.Default.PCAppConfig == "PCB")
{
// create new PCB form
// PCBForm.ShowDialog();
}
else if (Properties.Settings.Default.PCAppConfig == "SUB")
{
// create new SUB form
// SUBForm.ShowDialog();
}
--- 在 jtijn 的解决方案中,我尝试添加另一个名为 VersionNum 的属性,但失败了...... ---
<Target Name="BeforeBuild">
<PropertyGroup>
<!--Default value.-->
<PcAppConfig Condition="'$(PcAppConfig)' == ''">PCB</PcAppConfig>
<VersionNum Condition="'$(VersionNum)' == ''">1.0.0.66</VersionNum>
<!--The source code.-->
<TheSourceCode>internal static class PcAppConfig{ public static readonly string value = "$(PcAppConfig)"%3B%0A }internal static class VersionNum{ public static readonly string value = "$(VersionNum)"%3B</TheSourceCode>
</PropertyGroup>
<!--Get current source so we can only create it again when needed, to avoid being recompiled.-->
<ReadLinesFromFile File="PcAppConfig.cs" ContinueOnError="True">
<Output TaskParameter="Lines" ItemName="CurrentSourceCode" />
</ReadLinesFromFile>
<!--Write source, if needed.-->
<WriteLinesToFile File="PcAppConfig.cs" Overwrite="True" Lines="$(TheSourceCode)" Condition="'@(CurrentSourceCode)' != '$(TheSourceCode)'" />
</Target>
--- 值改变时不编译,创建设置文件失败... 我对 MSBuild.exe 的论点
SelectApp.csproj.user /p:PcAppConfig=SUB /p:VersionNum=1.0.0.67
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Update="Form1.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
<Target Name="BeforeBuild">
<PropertyGroup>
<!--Default value.-->
<PcAppConfig Condition="'$(PcAppConfig)' == ''">PCB</PcAppConfig>
<VersionNum Condition="'$(VersionNum)' == ''">1.0.0.66</VersionNum>
<!--The source code.-->
<TheSourceCode>internal static class PCAppConfig{ public static readonly string value = "$(PcAppConfig)"%3B }%0Ainternal static class VersionNum{ public static readonly string value = "$(VersionNum)"%3B }</TheSourceCode>
</PropertyGroup>
<!--Get current source so we can only create it again when needed, to avoid being recompiled.-->
<ReadLinesFromFile File="PcAppConfig.cs" ContinueOnError="True">
<Output TaskParameter="Lines" ItemName="CurrentSourceCode" />
</ReadLinesFromFile>
<!--Write source, if needed.-->
<WriteLinesToFile File="PcAppConfig.cs" Overwrite="True" Lines="$(TheSourceCode)" Condition="'@(CurrentSourceCode)' != '$(TheSourceCode)'" />
<ItemGroup>
<Compile Update="PcAppConfig.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
</Target>
<Target Name="AfterCompile">
<Exec Command=""$(ProgramFiles)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.com" C:\DummyApps\SelectApp\PCBSetup\PCBSetup.vdproj /build "Debug|AnyCPU""/>
</Target>
</Project>
【问题讨论】:
-
应用程序设置是运行时属性,在运行时从文件中加载,在构建时尝试设置它几乎没有意义。并且 MSBuild 属性与应用程序设置完全不同,这就是您的尝试无效的原因。但你真正想做的是什么?更改设置的默认值?在构建时设置一个常量(不是设置)?
-
是的,我明白了。我试图“优雅地”尝试使用属性或变量来检查 Winform Form 类,并根据运行时的值(PCB、SUB 等),创建另一个 Form 的实例并显示它。我不仅需要该属性,还需要一个可以用来保存版本号的字符串。运行时的应用程序设置将起作用,但我不想每次我想根据该属性的值编译应用程序时都更改属性值。
-
如果我理解正确,您希望能够构建不同版本的应用程序(其中某些字符串常量不同)?
-
是的,我相信这是正确的。因此,例如在我的主 Form 类中的 Load() 方法中......但是使用某种构建方法,我可以构建项目和相关的设置项目,可能使用 MSBuild.exe。请参阅上面的描述,了解我在此评论中的意思,
标签: visual-studio msbuild