【问题标题】:Loop through Embedded Resources and copy to local path循环通过嵌入式资源并复制到本地路径
【发布时间】:2016-05-06 23:14:15
【问题描述】:

我有一个简单的 WinForms 应用程序,但它有一些嵌入式资源(在“资源”下的子文件夹中),我想将它们复制到计算机上的文件夹中。目前,我让后者工作(使用明确的方法命名嵌入式资源及其应该去哪里):

string path = @"C:\Users\derek.antrican\";

using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("WINFORMSAPP.Resources.SUBFOLDER.FILE.txt"))
using (Stream output = File.Create(path + "FILE.txt"))
{
    input.CopyTo(output);
}

但我仍在试图弄清楚如何让前者工作:遍历“WINFORMSAPP.Resources.SUBFOLDER”文件夹中的所有资源并移动它们。我已经做了很多谷歌搜索,但我仍然不确定如何获取此子文件夹中每个嵌入式资源的列表。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# winforms resources embedded-resource


    【解决方案1】:

    首先将所有资源嵌入到您的程序集中:

    Assembly.GetExecutingAssembly().GetManifestResourceNames()
    

    您可以通过简单地调用StartsWith 来检查这些名称与所需子文件夹的名称,看看它们是在里面还是外面。

    现在遍历名称,并获取相应的资源流:

    const string subfolder = "WINFORMSAPP.Resources.SUBFOLDER.";
    var assembly = Assembly.GetExecutingAssembly();
    foreach (var name in assembly.GetManifestResourceNames()) {
        // Skip names outside of your desired subfolder
        if (!name.StartsWith(subfolder)) {
            continue;
        }
        using (Stream input = assembly.GetManifestResourceStream(name))
        using (Stream output = File.Create(path + name.Substring(subfolder.Length))) {
            input.CopyTo(output);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-10
      • 2018-03-08
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多