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

如何阻止Visual Studio 2022保存.resx时修改.csproj文件?

问题描述

我用Visual Studio 2022创建了一个C#控制台应用,添加了多个.resx文件并修改了.csproj文件来批量配置资源文件的自动代码生成,原始配置如下:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Compile Update="**/*.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>$([System.String]::Copy("%(Filename)").Replace(".Designer", "")).resx</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="**/*.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>$([System.String]::Copy("%(Filename)").Replace(".resx", ".Designer.cs"))</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>

</Project>

这个配置的目的是让Visual Studio自动为所有.resx文件生成对应的.Designer.cs文件,无需逐个手动配置,目前运行正常。但每次在Visual Studio 2022中修改任何资源文件后,.csproj会被自动修改,添加被修改资源的显式配置项,比如修改Resource1.resx后,csproj会多出以下内容:

<ItemGroup>
    <Compile Update="Resource1.Designer.cs">
      <DesignTime>True</DesignTime>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="Resource1.resx">
      <LastGenOutput>Resource1.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>

这些显式配置项并不是我需要的,请问有没有办法禁用这种自动添加行为?优先通过.csproj配置或Visual Studio设置实现。

解决方案

1. 通过.csproj配置禁用自动添加显式项

在项目文件的<PropertyGroup>中添加以下配置,阻止Visual Studio自动将单个资源文件的配置写入csproj:

<PropertyGroup>
  <!-- 关闭默认的文件自动包含规则,避免单个资源文件的显式配置被添加 -->
  <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
  <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

注意:该设置会关闭所有文件的默认自动包含逻辑,因此需要手动补充其他类型文件的通配符配置,比如所有非Designer的C#代码文件:

<ItemGroup>
  <Compile Include="**/*.cs" Exclude="**/*.Designer.cs" />
</ItemGroup>

2. 修改Visual Studio全局设置

打开Visual Studio的工具 > 选项,依次进入项目和解决方案 > .NET Core,取消勾选自动将文件添加到项目中选项。这个设置会全局阻止Visual Studio自动向csproj中添加单个文件的显式配置,包括资源文件修改后的自动添加行为。

3. 使用Directory.Build.props统一批量配置

如果有多个项目需要相同的资源文件配置,可以在解决方案根目录创建Directory.Build.props文件,内容如下:

<Project>
  <PropertyGroup>
    <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="**/*.cs" Exclude="**/*.Designer.cs" />
    <Compile Update="**/*.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>$([System.String]::Copy("%(Filename)").Replace(".Designer", "")).resx</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="**/*.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>$([System.String]::Copy("%(Filename)").Replace(".resx", ".Designer.cs"))</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>
</Project>

所有子项目会自动继承该配置,无需逐个修改csproj文件。

内容的提问来源于stack exchange,提问作者Zvonimir Matic

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 08:50:42