开发者

Is there way to read a text file from an assembly by using Reflection in C#?

开发者 https://www.devze.com 2022-12-10 00:02 出处:网络
I have a text file inside the assembly say MyAssembly. I am trying to access that text file from the code like this :

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.Get开发者_如何学CExecutingAssembly();
           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


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.


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).


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();

0

精彩评论

暂无评论...
验证码 换一张
取 消