gpt4 book ai didi

c# - 遍历嵌入式资源并复制到本地路径

转载 作者:行者123 更新时间:2023-11-30 14:50:16 24 4
gpt4 key购买 nike

我有一个简单的 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”文件夹中的所有资源并移动它们。我已经做了很多谷歌搜索,但我仍然不确定如何获取此子文件夹中每个嵌入式资源的列表。

如有任何帮助,我们将不胜感激!

最佳答案

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

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);
}
}

关于c# - 遍历嵌入式资源并复制到本地路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37082689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com