I just want to extract some embedded resources or a list of them from my assembly to a directory. I used File.Stream
, WriteAllBytes
and also File.Copy
, but it did NOT work, and its only an output with 0 bytes of size. Whats the problem with this?
public void Main()
{
Assembly.GetExecutingAssembly().GetManifestResourceNames();
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myNameSpace.myEmbeddedRes");
FileStream fileStream = new FileStream("e:\\new.txt", FileMode.CreateNew, FileAccess.Write);
for (int i = 0; i < stream.Length; i++)
fileStream.WriteByte((byte)stream.ReadByte());
fileStream.Close();
}
or
private static void ExtEmbdRes(string outDir, string resLoc, List<string> files)
{
foreach (string file in files)
{
using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resLoc + "." + file))
{
using (System.IO.FileStream fileStream = new System.IO.FileStream(System.IO.Path.Combine(outDir, file), System.IO.FileMode.Create))
{
for (int i = 0; i < stream.Length; i++)
{
fileStream.WriteByte((byte)stream.ReadByte());
}
fileStream.Close();
}
}
}
}
static void Main()
{
List<string> fileList = new List<string>();
{
fileList.Add("txtFile1.txt");
fileList.Add("txtFile2.txt");
fileList.Add("txtFile3.txt");
};
ExtEmbdRes("e:\\", "myNameSpace", fileList);
}
You can use assembly.GetManifestResourceNames() to Get a list of embedded Resources. Also are you sure the executing assembly is the one with the embedded resources? Perhaps you are using a class library