以编程方式为resx文件生成Designer.cs(ResXResourceWriter / ResXResourceReader)

时间:2010-09-16 17:42:50

标签: c# resx resgen

我正在使用ResXResourceWriter / ResXResourceReader在TFS中创建/更新resx文件,它不会生成.Designer.cs文件。我看到Resgen创建了.Designer.cs。如何以编程方式调用以在某个TFS文件路径生成.Designer.cs?这是这样的吗?

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin\ResGen.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Minimized;
                startInfo.Arguments = "ResourceName.resx /publicClass /str:cs, Namespace, ResourceName, ResourceName.Designer.cs";
                Process.Start(startInfo);

1 个答案:

答案 0 :(得分:17)

我发现了如何以编程方式生成.Designer.cs文件。

string[] unmatchedElements;
var codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.CodeCompileUnit code =
    System.Resources.Tools.StronglyTypedResourceBuilder.Create(
        "MyClass.resx", "MyClass", "my.namespace", codeProvider, 
        true, out unmatchedElements); // Needs System.Design.dll

using(StreamWriter writer = new StreamWriter("MyClass.Designer.cs", false,
    System.Text.Encoding.UTF8))
{
    codeProvider.GenerateCodeFromCompileUnit(code, writer,
        new System.CodeDom.Compiler.CodeGeneratorOptions());
}

// Returns false if at least one ppty couldn't be generated.
return unmatchedElements.Length == 0;