如何从C#程序集中提取嵌入资源列表?

时间:2017-12-23 10:17:33

标签: c# embedded-resource

我只是想从程序集中提取一些嵌入资源或它们的列表到目录。我使用了File.StreamWriteAllBytesFile.Copy,但它没有用,它只有0字节大小的输出。问题是什么?

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();
}

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);
}

1 个答案:

答案 0 :(得分:1)

您可以使用assembly.GetManifestResourceNames()获取嵌入的资源列表。您还确定执行程序集是具有嵌入资源的程序集吗?也许您正在使用类库