0

This code is a simplified to aid my understanding. The actual code will be finding an xml file in a calling assembly. However, I can't even access an xml file in this simplified example. I have set the TestFile.xml as an Embedded Resource. Any ideas why this code doesn't work?

using System;
using System.Reflection;

namespace Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new A();
            test.TestGettingAssemblies();

            Console.ReadKey();
        }
    }

    internal class A
    {
        internal void TestGettingAssemblies()
        {
            Console.WriteLine(Assembly.GetExecutingAssembly().FullName);
            Console.WriteLine(Assembly.GetExecutingAssembly().GetFile("TestFile.xml") == null);
            Console.WriteLine(Assembly.GetCallingAssembly().FullName);
        }
    }
}

Code, Results

Kaman Poole
  • 87
  • 1
  • 10

1 Answers1

0

Solution:

var assembly = Assembly.GetExecutingAssembly();
using(var stream = assembly.GetManifestResourceStream("TestFile.xml"))
using(TextReader tr = new StreamReader(stream))
{
      Console.WriteLine(tr.ReadToEnd());
}

For more information go here.

It it does not work, I would recommend you to call assembly.GetManifestResourceNames() and check if result contanins "TestFile.xml"

Kedrzu
  • 623
  • 5
  • 12