【问题标题】:Why can't I access a file using GetExecutingAssembly.GetFile()? [duplicate]为什么我不能使用 GetExecutingAssembly.GetFile() 访问文件? [复制]
【发布时间】:2016-11-25 15:34:02
【问题描述】:

此代码是为了帮助我理解而简化的。实际代码将在调用程序集中查找 xml 文件。但是,在这个简化的示例中,我什至无法访问 xml 文件。我已将 TestFile.xml 设置为嵌入式资源。 任何想法为什么这段代码不起作用?

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

【问题讨论】:

  • 感谢格式化帮助。

标签: c# assemblies


【解决方案1】:

解决方案:

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

欲了解更多信息,请访问here

它不起作用,我建议您致电assembly.GetManifestResourceNames() 并检查结果是否包含“TestFile.xml”

【讨论】:

  • 谢谢。我能够通过 GetManifestResouceStream 获得它。奇怪的是 GetFile 不起作用。
猜你喜欢
  • 2017-02-28
  • 2016-01-19
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 2019-05-04
  • 2014-12-15
  • 2019-09-07
  • 1970-01-01
相关资源
最近更新 更多