0

我正在尝试从程序集中读取 xml 文件,这是我的代码:

private Assembly testAssembly;
    private void button1_Click(object sender, EventArgs e)
    {
        string testfile = "stack.xml";
        String testfileName = string.Format("{0}.{1}", testAssembly.GetName().Name, testfile);

        using (Stream stream = testAssembly.GetManifestResourceStream(testfileName))
        {
            using (StreamReader sr = new StreamReader(stream))
            {
                var value = sr.ReadToEnd();
                Console.WriteLine("The read file is {0}", value);
            }
        }
    }

我在这条线上有一个问题:

String testfileName = string.Format("{0}.{1}", testAssembly.GetName().Name, 
                                     testfile); 

未处理的空引用异常,它说空。有什么帮助??? 在此处输入图像描述

4

2 回答 2

2

看起来marksAssemblynull

于 2012-10-17T14:38:48.427 回答
1
string resName = "resources/HistoryConnections.xml";  

private static UnmanagedMemoryStream GetResourceStream(string resName)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var strResources = assembly.GetName().Name + ".g.resources";
            var rStream = assembly.GetManifestResourceStream(strResources);
            var resourceReader = new ResourceReader(rStream);
            var items = resourceReader.OfType<DictionaryEntry>();
            var stream = items.First(x => (x.Key as string) == resName.ToLower()).Value;
            return (UnmanagedMemoryStream)stream;
        }
于 2015-04-28T07:02:22.063 回答