作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 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/
我是一名优秀的程序员,十分优秀!