tags:

views:

391

answers:

3

I have a text file inside the assembly say MyAssembly. I am trying to access that text file from the code like this :

Stream stream = Assembly.GetAssembly(typeof(MyClass)).GetFile("data");

where data is data.txt file containing some data and I have added that .txt as Embedded Resources. I have dome reading of the images from the Assebly as embedded resources with code like this :

protected Stream GetLogoImageStream()
       {

           Assembly current = Assembly.GetExecutingAssembly();
           string imageFileNameFormat = "{0}.{1}";
          string imageName = "myLogo.GIF";

           string assemblyName = current.ManifestModule.Name;
           int extensionIndex = assemblyName.LastIndexOf(".dll", StringComparison.CurrentCultureIgnoreCase);
           string file = string.Format(imageFileNameFormat, assemblyName.Remove(extensionIndex, 4), imageName);            
           Stream thisImageStream = current.GetManifestResourceStream(file);
           return thisImageStream;

       }

However, this approach did not work while reading the .txt file from an the executing assembly. I would really appreciate if anybody can point me to the approach to read .txt file from an assembly. Please dont ask me why I am not reading the file from the drive or the network share. Just say that the requirement is to read the .txt file from the Assembly.

Thank you so much

A: 

Your approach should work. GetManifestResourceStream returns null, if the resource is not found. Try checking the run-time value of your file variable with the actual name of the resource stored in the assembly (you could check it using Reflector).

driis
+3  A: 

GetManifestResourceStream is indeed the correct way to read the data. However, when it returns null, that usually means you have specified the wrong name. Specifying the correct name is not as simple as it seems. The rules are:

  • The VB.NET compiler generates a resource name of <root namespace>.<physical filename>.
  • The C# compiler generates a resource name of <default namespace>.<folder location>.<physical filename>, where <folder location> is the relative folder path of the file within the project, using dots as path separators.

You can call the Assembly.GetManifestResourceNames method in the debugger to check the actual names generated by the compiler.

Christian Hayter
A: 

I really appreciate for everybody's help on this question. I was able to read the file with the code like this :

Assembly a = Assembly.GetExecutingAssembly();
            string[] nameList = a.GetManifestResourceNames();
            string manifestanme = string.Empty;
            if (nameList != null && nameList.Length > 0)
            {
                foreach (string name in nameList)
                {
                    if (name.IndexOf("c.txt") != -1)
                    {
                        manifestanme = name;
                        break;
                    }
                }
            }

            Stream stream = a.GetManifestResourceStream(manifestanme);

Thanks and +1 for Christian Hayter for this method : a.GetManifestResourceNames();

Shiva