简体   繁体   中英

How to extract a list of embedded resources from C# assembly?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM