如何使用以下代码仅提取除命名空间之外的文件名?目前,此代码包含GetManifestResourceNames()的整个命名空间。
Assembly assembly = System.Reflection.Assembly.LoadFile(resourceLocation + @"\\" + file);
string[] names = assembly.GetManifestResourceNames();
foreach (var name in names.Where(x => x.EndsWith("xsd")).ToList())
{
using (System.IO.Stream stream = assembly.GetManifestResourceStream(name))
{
using (System.IO.FileStream fileStream = new System.IO.FileStream(System.IO.Path.Combine(outputDir, name), System.IO.FileMode.Create))
答案 0 :(得分:1)
您可以使用GetManifestResourceInfo获取其他信息,例如文件名。
举个例子,你可能会得到如下内容:
string[] names = assembly.GetManifestResourceNames();
foreach (var name in names.Where(x => x.EndsWith("xsd")).ToList())
{
var info = assembly.GetManifestResourceInfo(name);
if (info != null)
{
var fileName = info.FileName;
// Do your stuff that needs filename here.
}
}
编辑:如果在这些情况下发现GetManifestResourceInfo返回null,则此SO帖子可能是相关的:Why does GetManifestResourceStream returns null while the resource name exists when calling GetManifestResourceNames?
应该将资源设置为构建“嵌入式资源”的操作,并且这里提到了代码安全性问题:http://adrianmejia.com/blog/2011/07/18/cs-getmanifestresourcestream-gotcha/