0

I have added a text file resource into my winform C# project in Resource Folder Here the content of Resources.resx after I add

<data name="_0" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\0.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>

and then I try to read the content of that embedded resourde using this solution before

How to read embedded resource text file

Here is the main program

        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "Properties.Resources.0.txt"; 
        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        try
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string result = reader.ReadToEnd();
                Console.WriteLine(result);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

but I got null parameter in var reader, I think it's about the path of resource name, any IDEA to solve my problem? thanks before

4

1 回答 1

0

You have to append YOUR project namespace to the resourcename:

var resourceName = "Properties.Resources.0.txt"; 

Change it to:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "ProjectNamespace.Properties.Resources.0.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}
于 2013-10-15T06:52:19.677 回答