如何在运行时C#中搜索嵌入文件

时间:2012-03-08 12:38:47

标签: c# reflection .net-assembly

是否可以搜索符合特定搜索条件的嵌入文件(例如* .xml)。

我的代码(仅适用于我知道路径的文件)

var fileName = "PATH.sample.xml";
var assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(fileName);

StreamReader reader = new StreamReader(stream);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(reader.ReadToEnd());

2 个答案:

答案 0 :(得分:5)

var findedResourceNames = assembly.GetManifestResourceNames()
     .Where(name => name.EndsWith(".xml")).ToArray();

答案 1 :(得分:1)

string[] files = System.IO.Directory.GetFiles("yourPath", "*.xml");

foreach (string file in files)
{
   //do what you need here
}