且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将本地化资源.DLL嵌入C#中的可执行文件?

更新时间:2023-02-16 14:49:55

在.NET Framework 4中,您可以将资源库嵌入到可执行文件中。



http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx



只需创建相同的结构(使用本地化文件夹'lib / en','lib / de')并嵌入它们。

  private static Assembly MyResolveEventHandler(object sender,ResolveEventArgs args){
AssemblyName MissingAssembly = new AssemblyName(args.Name);
CultureInfo ci = MissingAssembly.CultureInfo;

...
resourceName =MyApp.lib。 + ci.Name.Replace( - ,_)+。 + MissingAssembly.Name +.dll;
var stream = Assembly.GetExecutingAssembly()。GetManifestResourceStream(resourceName)
...
}


I want to make my program multilingual. I have successfully made the program multilingual via Form's Localizable and Language properties. It made some .resx files. Then I deleted non-needed files such as images (which they are the same in all langauges) etc from the .resx files.

The problem is, for example, it also generates a folder called "en" and in that folder, another generated file is called "ProjectName.resources.dll".

Is there anyway to embed this resource file to the .exe? Adding it to the resources and setting Build Action to "Embedded Resource" doesn't work also.

Thanks.

In .NET Framework 4 you can embed resource library into executable.

http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx

Just create same structure ( with localized folders 'lib/en', 'lib/de' ) and embed them.

private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
    AssemblyName MissingAssembly = new AssemblyName(args.Name);
    CultureInfo ci = MissingAssembly.CultureInfo;

    ...
    resourceName = "MyApp.lib." + ci.Name.Replace("-","_") + "." + MissingAssembly.Name + ".dll";
    var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
    ...
}