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

Community
  • 1
  • 1
rifleon
  • 105
  • 1
  • 1
  • 10
  • Paste the code you are using to read the embedded file please – Carlos Landeras Oct 15 '13 at 06:37
  • Oh I finally solved, the problem is my file embedded has no include in resource list, to insert in resource list you can right click in the file (in Solution Explorer Visual Studio), and then change the build action properties into Embedded Resource To view resourcename you can use method String[] list = assembly.GetManifestResourceNames(); – rifleon Oct 15 '13 at 08:05

1 Answers1

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();
}
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • then does the namespace is same with project name I created? For example if I create project ReadResource, then it will same as ProjectNamespace name? If so, I have changed but not change the result – rifleon Oct 15 '13 at 06:58
  • The namespace is defined by you. But if you did not changed it, it is the project name. For example if I create a project named: SportSTore and add a Resource called myFile.txt. The full resource path is SportStore.Properties.myFile.txt – Carlos Landeras Oct 15 '13 at 07:07