如何从资源文件获取流对象(控制台应用程序/ Windows服务项目) [英] How To Get A Stream Object From A Resource File (Console App/Windows Service Project)

查看:75
本文介绍了如何从资源文件获取流对象(控制台应用程序/ Windows服务项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Windows服务,并试图访问添加到资源文件中的某些文件,但是由于我不知道如何访问单个文件,因此我陷入了困境。仅出于一些背景信息,这是我到目前为止所做的:

I'm creating a Windows service and am trying to access some files I added to a resource file, but I'm stuck because I don't know how to access the individual files. Just for some background info, here's what I've done so far:


  • 这是在调试中运行的C#Windows Service应用程序

  • This is a C# Windows Service application running in debug mode as a console app, which helps me step into the code.

我将一个资源文件添加到了名为 Resources.resx的根目录。

I added a resource file to the root called "Resources.resx".

在我的资源文件中,我使用视觉设计器/编辑器添加了一些jpg图像和html文件。

In my resource file, I added a few jpg images and html files using the visual designer/editor.

在将图像和html文件添加到资源文件后,项目中出现了一个名为资源的新文件夹,其中包含我添加的所有文件。

After I added the images and html files to the resource file, a new folder in my project appeared named "Resources" with all the files I added.

在这个新文件夹中,我转到每个文件的属性,并将生成操作更改为嵌入式资源。 (我不知道这是否有必要。我搜索过的一些博客说可以尝试。)

In this new folder, I went to the properties of each file and changed the Build Action to Embedded Resource. (I don't know if this is necessary. Some blog I searched said to try it.)

该项目的名称空间称为 MicroSecurity.EmailService。

The project's namespace is called "MicroSecurity.EmailService".

为了获取资源文件的名称,我使用了

In order to get the name of the resource file, I used

GetType()。 Assembly.GetManifestResourceNames()

GetType().Assembly.GetManifestResourceNames()

我得到以下内容

GetType()。Assembly.GetManifestResourceNames(){string [2]}字符串[]
[0] MicroSecurity.EmailService.Services.EmailService.resources字符串
[1] MicroSecurity.EmailService .resources.resources字符串

GetType().Assembly.GetManifestResourceNames() {string[2]} string[] [0] "MicroSecurity.EmailService.Services.EmailService.resources" string [1] "MicroSecurity.EmailService.Resources.resources" string

由此,我确定 MicroSecurity.EmailService.Resources.resources是我要使用的字符串(索引1)。

From this I identified that "MicroSecurity.EmailService.Resources.resources" is the string I want to use (index 1).


  • 我使用以下代码获取流对象。

  • I used the this code to get a stream object.

var stream = Assembly.GetExecutingAssembly()。GetManifestResourceStream( MicroSecurity.EmailService.Resources.resources);

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MicroSecurity.EmailService.Resources.resources");

当我添加w在调试过程中使用此变量,我可以看到诸如图像元数据之类的东西。

When I add a watch to this variable during debugging, I can see things such as metadata for my images and etc.

在这里,我被困住了。我想访问名为 logo.jpg的图像。这是我要获取的图像,但是不起作用。

Here is where I'm stuck. I would like to access the image called "logo.jpg". This is what I'm doing to get at the image, but it's not working.

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MicroSecurity.EmailService.Resources.resources.logo.jpg");

如何从我的logo.jpg文件中获取流?

How can I get a stream from my logo.jpg file?

更新:

多亏了安德鲁,我才知道了。下面是我为演示项目编写的一些代码,目的是研究资源文件的工作方式与直接嵌入文件的方式。我希望这可以帮助其他人弄清差异。

Thanks to Andrew, I was able to figure it out. Below is some code I wrote for a demo project in order to study how the resource file works vs. embedding files directly. I hope this helps others to clarify the differences.

using System;
using System.Drawing;
using System.IO;
using System.Reflection;

namespace UsingResourceFiles
{
    public class Program
    {
        /// <summary>
        /// Enum to indicate what type of file a resource is.
        /// </summary>
        public enum FileType
        {
            /// <summary>
            /// The resource is an image.
            /// </summary>
            Image,

            /// <summary>
            /// The resource is something other than an image or text file.
            /// </summary>
            Other,

            /// <summary>
            /// The resource is a text file.
            /// </summary>
            Text,           
        }

        public static void Main(string[] args)
        {
            // There are two ways to reference resource files:
            // 1. Use embedded objects.
            // 2. Use a resource file.

            // Get the embedded resource files in the Images and Text folders.
            UseEmbeddedObjects();

            // Get the embedded resource files in the Images and Text folders. This allows for dynamic typing
            // so the resource file can be returned either as a stream or an object in its native format.
            UseEmbeddedObjectsViaGetResource();

            // Use the zombie.gif and TextFile.txt in the Resources.resx file.
            UseResourceFile();
        }

        public static void UseEmbeddedObjects()
        { 
            // =============================================================================================================================
            //
            //                                                     -=[ Embedded Objects ]=-
            //
            // This way is the easiest to accomplish. You simply add a file to your project in the directory of your choice and then 
            // right-click the file and change the "Build Action" to "Embedded Resource". When you reference the file, it will be as an 
            // unmanaged stream. In order to access the stream, you'll need to use the GetManifestResourceStream() method. This method needs
            // the name of the file in order to open it. The name is in the following format:
            //
            // Namespace + Folder Path + File Name
            //
            // For example, in this project the namespace is "UsingResourceFiles", the folder path is "Images" and the file name is 
            // "zombie.gif". The string is "UsingResourceFiles.Images.zombie.gif". 
            //
            // For images, once the image is in a stream, you'll have to convert it into a Bitmap object in order to use it as an Image
            // object. For text, you'll need to use a StreamReader to get the text file's text.
            // =============================================================================================================================
            var imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("UsingResourceFiles.Images.zombie.gif");
            var image = new Bitmap(imageStream);

            var textStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("UsingResourceFiles.Text.TextFile.txt");
            var text = new StreamReader(textStream).ReadToEnd();
        }

        public static void UseEmbeddedObjectsViaGetResource()
        {
            // =============================================================================================================================
            //
            //                                             -=[ Embedded Objects Using GetResource() ]=-
            //
            // Using the overloaded GetResource() method, you can easily obtain an embedded resource file by specifying the dot file path
            // and type. If you need the stream version of the file, pass in false to the useNativeFormat argument. If you use the
            // GetResource() method outside of this file and are getting a null value back, make sure you set the resource's "Build Action"
            // to "Embedded Resource".
            // =============================================================================================================================

            // Use the GetResource() methods to obtain the Images\zombie.gif file and the text from the Text\TextFile.txt file.
            Bitmap image = GetResource("Images.zombie.gif", FileType.Image);
            Stream imageStream = GetResource("Images.zombie.gif", FileType.Image, false);

            string text = GetResource("Text.TextFile.txt", FileType.Text);
            Stream textStream = GetResource("Text.TextFile.txt", FileType.Text, false);
        }

        public static void UseResourceFile()
        {
            // =============================================================================================================================
            //
            //                                                      -=[ Resource File ]=-
            //
            // This way takes more upfront work, but referencing the files is easier in the code-behind. One drawback to this approach is
            // that there is no way to organize your files in a folder structure; everything is stuffed into a single resource blob.
            // Another drawback is that once you create the resource file and add any files to it, a folder with the same name as your
            // resource file is created, creating clutter in your project. A final drawback is that the properties of the Resources object
            // may not follow proper C# naming conventions (e.g. "Resources.funny_man" instead of "Resources.FunnyMan"). A plus for using
            // resource files is that they allow for localization. However, if you're only going to use the resource file for storing files,
            // using the files as embedded objects is a better approach in my opinion.
            // =============================================================================================================================

            // The Resources object references the resource file called "Resources.resx".
            // Images come back as Bitmap objects and text files come back as string objects.
            var image = Resources.zombie;
            var text = Resources.TextFile;
        }

        /// <summary>
        /// This method allows you to specify the dot file path and type of the resource file and return it in its native format.
        /// </summary>
        /// <param name="dotFilePath">The file path with dots instead of backslashes. e.g. Images.zombie.gif instead of Images\zombie.gif</param>
        /// <param name="fileType">The type of file the resource is.</param>
        /// <returns>Returns the resource in its native format.</returns>
        public static dynamic GetResource(string dotFilePath, FileType fileType)
        {
            try
            {
                var assembly = Assembly.GetExecutingAssembly();
                var assemblyName = assembly.GetName().Name;
                var stream = assembly.GetManifestResourceStream(assemblyName + "." + dotFilePath);
                switch (fileType)
                { 
                    case FileType.Image:                    
                        return new Bitmap(stream);
                    case FileType.Text:
                        return new StreamReader(stream).ReadToEnd();
                    default:
                        return stream;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }

        /// <summary>
        /// This method allows you to specify the dot file path and type of the resource file and return it in its native format.
        /// </summary>
        /// <param name="dotFilePath">The file path with dots instead of backslashes. e.g. Images.zombie.gif instead of Images\zombie.gif</param>
        /// <param name="fileType">The type of file the resource is.</param>
        /// <param name="useNativeFormat">Indicates that the resource is to be returned as resource's native format or as a stream.</param>
        /// <returns>When "useNativeFormat" is true, returns the resource in its native format. Otherwise it returns the resource as a stream.</returns>
        public static dynamic GetResource(string dotFilePath, FileType fileType, bool useNativeFormat)
        {
            try
            {
                if (useNativeFormat)
                {
                    return GetResource(dotFilePath, fileType);
                }

                var assembly = Assembly.GetExecutingAssembly();
                var assemblyName = assembly.GetName().Name;
                return assembly.GetManifestResourceStream(assemblyName + "." + dotFilePath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }
    }
}


推荐答案

如果将Resources文件夹中的文件设置为Embedded Resource,则应该已经在GetManifestResourceNames()调用中看到了它。您可以尝试

If you set the files in the Resources folder to Embedded Resource then you should have seen it listed in the GetManifestResourceNames() call. You could try

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MicroSecurity.EmailService.Resources.logo.jpg");

如果在资源中,则名称应为 MicroSecurity.EmailService.Resources.logo.jpg夹。但是,将文件本身标记为嵌入式资源会破坏资源文件的目的(图像本身会被嵌入两次)。

The name should be "MicroSecurity.EmailService.Resources.logo.jpg" if it is in the Resources folder. However, marking the file itself as an Embedded Resource defeats the purpose of the Resources file (the image itself would be embedded twice).

您可以完全删除资源文件,然后将每个文件设置为嵌入式资源。那时,每个文件应该有单独的清单资源。在C#项目中,每个文件名都将以项目名称空间+子文件夹为前缀。例如。如果在资源/嵌入式文件夹中添加 logo.jpg文件,则资源名称将为 MicroSecurity.EmailService.Resources.Embedded.logo.jpg。

You can remove the resources file entirely and set each file as an Embedded Resource. At that point, there should be separate manifest resources for each file. In a C# project, each file name will be prefixed by the project namespace + the sub folder. Eg. if you add a "logo.jpg" file in a Resources/Embedded folder, the resource name will be "MicroSecurity.EmailService.Resources.Embedded.logo.jpg".

或者,从资源文件中获取位图并将其转换为流。您可以在位图转换为 MemoryStream 的示例。 com / questions / 268013 / how-do-i-convert-a-bitmap-to-byte>如何将位图转换为byte []?

Alternatively, get the bitmap from the Resources file and convert it to a stream. You can find an example of converting a Bitmap to a MemoryStream in How do I convert a Bitmap to byte[]?

这篇关于如何从资源文件获取流对象(控制台应用程序/ Windows服务项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆