如何在Visual Studio外部运行PublicResXFileCodeGenerator自定义工具
手动触发VS2022的PublicResXFileCodeGenerator更新设计器文件
方法一:使用MSBuild命令行
VS2022自带的MSBuild和内部使用逻辑一致,能可靠触发自定义工具执行:
- 找到VS2022的MSBuild路径,替换
<Edition>为你的VS版本(如Community/Professional/Enterprise):C:\Program Files\Microsoft Visual Studio\2022\<Edition>\MSBuild\Current\Bin\MSBuild.exe - 切换到项目目录,执行命令更新所有关联resx的设计器文件:
MSBuild.exe YourProject.csproj /t:UpdateDesignerFiles /p:ResXFileCodeGenerator=PublicResXFileCodeGenerator - 若只需更新单个resx文件,用以下命令:
MSBuild.exe YourProject.csproj /t:GenerateResource /p:InputFiles=Resources.resx /p:OutputFile=Resources.Designer.cs /p:CodeGenerator=PublicResXFileCodeGenerator
方法二:直接调用VS自定义工具程序集
PublicResXFileCodeGenerator封装在VS的专属程序集中,可通过反射调用:
- 找到程序集路径,替换
<Edition>为你的VS版本:C:\Program Files\Microsoft Visual Studio\2022\<Edition>\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.ResXFileCodeGenerator.dll - 编写简单C#控制台程序调用生成逻辑(示例代码):
编译时需引用using System; using System.CodeDom.Compiler; using System.IO; using Microsoft.VisualStudio.ResXFileCodeGenerator; class Program { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("用法:ResXGenerator.exe <resx文件路径> <设计器文件路径>"); return; } var resxPath = args[0]; var designerPath = args[1]; var generator = new PublicResXFileCodeGenerator(); var provider = CodeDomProvider.CreateProvider("CSharp"); var options = new CodeGeneratorOptions(); using (var reader = new StreamReader(resxPath)) using (var writer = new StreamWriter(designerPath)) { generator.GenerateCode(reader, writer, provider, options); } Console.WriteLine("设计器文件已更新"); } }Microsoft.VisualStudio.ResXFileCodeGenerator.dll和System.CodeDom.dll,运行时传入resx与设计器文件路径即可。
方法三:使用VS开发者命令提示符
打开“Developer Command Prompt for VS 2022”(开始菜单搜索),直接执行命令,无需手动定位MSBuild路径:
MSBuild YourProject.csproj /t:UpdateDesignerFiles
该命令会自动处理项目中所有关联自定义工具的resx文件,生成与VS2022内部一致版本的设计器代码。
内容的提问来源于stack exchange,提问作者Neil
相关产品推荐
相关产品推荐

