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

发布于 2024-12-05 10:48:55 字数 258 浏览 0 评论 0原文

我想让我的程序多语言化。我已经通过表单的本地化和语言属性成功地使程序成为多语言的。它制作了一些 .resx 文件。然后我从 .resx 文件中删除了不需要的文件,例如图像(它们在所有语言中都是相同的)等。

问题是,例如,它还生成一个名为“en”的文件夹,在该文件夹中,另一个生成的文件名为“ProjectName.resources.dll”。

有没有办法将此资源文件嵌入到 .exe 中?将其添加到资源并将构建操作设置为“嵌入资源”也不起作用。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-12-12 10:48: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)
    ...
}

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)
    ...
}
追我者格杀勿论 2024-12-12 10:48:55

您不久前问过这个问题并且您已经接受了答案,但我仍然会尝试提供另一种方法。我遇到了同样的问题,这就是我解决它的方法:

我将 dll 作为资源添加到我的 C# 项目中,并将此代码添加到我的主方法(启动主 winform 的方法)中。

public static void Main(string[] args)
{
    if (InitdeDEDll()) // Create dll if it's missing.
    {
        // Restart the application if the language-package was added
        Application.Restart();
        return;
    }
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new YOURMAINFORM());
}

private static bool InitdeDEDll() // Initialize the German de-DE DLL
{
    try
    {
        // Language of my package. This will be the name of the subfolder.
        string language = "de-DE";
        return TryCreateFileFromRessource(language, @"NAMEOFYOURDLL.dll",
            NAMESPACEOFYOURRESSOURCE.NAMEOFYOURDLLINRESSOURCEFILE);
    }
    catch (Exception)
    {
        return false;
    }
}

private static bool TryCreateFileFromRessource(string subfolder, string fileName, byte[] buffer)
{
    try
    {
        // path of the subfolder
        string subfolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + (subfolder != "" ? @"\" : "") + subfolder;

        // Create subfolder if it doesn't exist
        if (!Directory.Exists(subfolder))
            Directory.CreateDirectory(subfolderPath);


        fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\" + subfolder + (subfolder!=""?@"\":"") + fileName;
        if (!File.Exists(fileName)) // if the dll doesn't already exist, it has to be created
        {
            // Write dll
            Stream stream = File.Create(fileName);
            stream.Write(buffer, 0, buffer.GetLength(0));
            stream.Close();
        }
        else
        {
            return false;
        }
    }
    catch
    {
        return false;
    }

    return true;
}

注意

:如果文件夹和语言 dll 丢失,这将再次创建它,因此您不必再关心将文件夹和 dll 与 exe 文件一起复制。如果你想让它完全消失,这当然不是正确的方法。

You've asked this question a while ago and you've already accepted an answer, but still I'll try to provide an alternative way. I had the same problem and this is how I solved it:

I added the dll as a Ressource to my C#-Project and added this code to my Main-Method (the one that starts your main winform).

public static void Main(string[] args)
{
    if (InitdeDEDll()) // Create dll if it's missing.
    {
        // Restart the application if the language-package was added
        Application.Restart();
        return;
    }
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new YOURMAINFORM());
}

private static bool InitdeDEDll() // Initialize the German de-DE DLL
{
    try
    {
        // Language of my package. This will be the name of the subfolder.
        string language = "de-DE";
        return TryCreateFileFromRessource(language, @"NAMEOFYOURDLL.dll",
            NAMESPACEOFYOURRESSOURCE.NAMEOFYOURDLLINRESSOURCEFILE);
    }
    catch (Exception)
    {
        return false;
    }
}

private static bool TryCreateFileFromRessource(string subfolder, string fileName, byte[] buffer)
{
    try
    {
        // path of the subfolder
        string subfolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + (subfolder != "" ? @"\" : "") + subfolder;

        // Create subfolder if it doesn't exist
        if (!Directory.Exists(subfolder))
            Directory.CreateDirectory(subfolderPath);


        fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\" + subfolder + (subfolder!=""?@"\":"") + fileName;
        if (!File.Exists(fileName)) // if the dll doesn't already exist, it has to be created
        {
            // Write dll
            Stream stream = File.Create(fileName);
            stream.Write(buffer, 0, buffer.GetLength(0));
            stream.Close();
        }
        else
        {
            return false;
        }
    }
    catch
    {
        return false;
    }

    return true;
}

}

Note: This will create the folder and language-dll again if it's missing, so you don't have to care anymore that you copy that folder and the dll with your exe-file. If you want it to be completely vanished this won't be the right approach of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文