c# - 基于定义C#更改exe图标

标签 c# icons exe

我正在为两个不同的人创建一个项目,我想通过定义更改图标。例如:

#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 文件,以便为两个客户创建不同的构建配置。例如,您可以执行以下操作:

  1. 通过右键单击 Visual Studio 中解决方案资源管理器中的项目并单击“卸载项目”来卸载项目。

  2. 右键单击已卸载的项目,然后单击“编辑”

  3. 找到“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>

  • 点击“保存”。

  • 在解决方案资源管理器中右键单击项目文件,然后单击“重新加载项目”。如果 Visual Studio 询问您是否要关闭项目文件,请选择"is"。

  • 现在,当您想要为不同的客户构建时,请转到“构建\配置管理器”并选择特定于客户的配置。 Configuration Manager with Customer-Specific Configuration

  • 重复这些步骤,为每个客户添加特定于版本的配置。
  • 关于c# - 基于定义C#更改exe图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976691/

    相关文章:

    python - pandas导入时exe文件运行失败

    c# - 缺少 Microsoft 时区

    ios - 快速为常规按钮提供条形按钮标识符

    c# - MVC Seed 不将数据放入数据库

    WPF - 如何将 XAML 图标放入 ImageSource 中?

    html - 如何将图标添加到 HTML 中?

    java - 我正在尝试创建一个界面,我们可以通过单击按钮执行 .exe 文件来安装软件

    c - 如何在exe程序上编辑特定的单词

    c# - 测试 Patch odata webapi 方法

    c# - 为什么 SQL Server CE Count() 返回负值?