我正在为两个不同的人创建一个项目,我想通过定义更改图标。例如:
#if customer1
//add code to select c:\path to resources\myimage1.ico for exe icon
#else
//add code to select c:\path to resources\myimage2.ico for exe icon
#endif
我知道您可以在此处手动选择所需的图标:
https://msdn.microsoft.com/en-us/library/339stzf7.aspx
但是定义方式对于我们使用 git 来说是有意义的,因此我们不必不断重新上传别人的图像。我们可以简单地放置定义并让它使用该图像。谢谢。
最佳答案
您可以修改 csproj 文件,以便为两个客户创建不同的构建配置。例如,您可以执行以下操作:
通过右键单击 Visual Studio 中解决方案资源管理器中的项目并单击“卸载项目”来卸载项目。
右键单击已卸载的项目,然后单击“编辑”
找到“ApplicationIcon”标记并将其替换为两个条件 PropertyGroup,如下所示:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer1|AnyCPU' ">
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer2|AnyCPU' ">
<ApplicationIcon>netfol.ico</ApplicationIcon>
</PropertyGroup>
这将为 Customer1 和 Customer2 创建调试构建配置。
- 在项目文件中的图标 ItemGroup 上执行相同的操作,如下所示:
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer1|AnyCPU' ">
<Content Include="icon.ico" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer2|AnyCPU' ">
<Content Include="netfol.ico" />
</ItemGroup>
- 查找配置本身的 PropertyGroup。它将有这一行:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
- 在此组下方,为两个新的调试客户配置添加调试配置组,如下所示:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer1|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug_Customer1\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer2|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug_Customer2\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
关于c# - 基于定义C#更改exe图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976691/