0

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);
}
2
  • Have you checked whether your resource file is defined as Embedded Resource? Commented Dec 23, 2017 at 10:19
  • Thanx for your instant, yes. Commented Dec 23, 2017 at 10:24

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

By twiddling with VS n code i finally found that apparently the manifest UAC elevating is affecting and When I do that without setting requestedExecutionLevel on requireAdministrator or so, it works properly! What to do now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.