der*_*can 3 c# resources embedded-resource winforms
我有一个简单的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);
}
Run Code Online (Sandbox Code Playgroud)
但我仍在试图找出如何使前者工作:循环遍历"WINFORMSAPP.Resources.SUBFOLDER"文件夹中的所有资源并移动它们.我已经完成了很多谷歌搜索,但我仍然不确定如何在这个子文件夹中获取每个嵌入式资源的列表.
任何帮助将不胜感激!
首先将所有资源嵌入到程序集中:
Assembly.GetExecutingAssembly().GetManifestResourceNames()
Run Code Online (Sandbox Code Playgroud)
您可以根据所需子文件夹的名称检查这些名称,以便通过简单的调用来查看它们是在内部还是外部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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
737 次 |
最近记录: |