2011-10-13 5 views
9

मैं एक विंडोज सेवा बना रहा हूं और संसाधन फ़ाइलों में जोड़े गए कुछ फ़ाइलों तक पहुंचने का प्रयास कर रहा हूं, लेकिन मैं अटक गया क्योंकि मैं डॉन हूं व्यक्तिगत फाइलों तक पहुंचने के बारे में नहीं पता। बस कुछ पृष्ठभूमि जानकारी के लिए, यहाँ मैं अब तक क्या किया है है:एक संसाधन फ़ाइल (कंसोल ऐप/विंडोज़ सेवा प्रोजेक्ट) से स्ट्रीम ऑब्जेक्ट कैसे प्राप्त करें

  • यह एक सी # Windows सेवा एक सांत्वना अनुप्रयोग है, जो मुझे कोड में कदम में मदद करता है के रूप में डिबग मोड में चल रहे अनुप्रयोग है।

  • मैंने "Resources.resx" नामक रूट पर एक संसाधन फ़ाइल जोड़ा।

  • मेरी संसाधन फ़ाइल में, मैंने दृश्य डिजाइनर/संपादक का उपयोग करके कुछ जेपीजी छवियों और एचटीएमएल फाइलों को जोड़ा।

  • संसाधन फ़ाइल में छवियों और एचटीएमएल फाइलों को जोड़ने के बाद, मेरे प्रोजेक्ट में एक नया फ़ोल्डर "संसाधन" नामक सभी फाइलों के साथ दिखाई दिया।

  • इस नए फ़ोल्डर में, मैं प्रत्येक फ़ाइल के गुणों पर गया और बिल्ड एक्शन को एंबेडेड संसाधन में बदल दिया। (मुझे नहीं पता कि यह जरूरी है या नहीं। मैंने जिस ब्लॉग को खोजा वह कोशिश करने के लिए कहा गया।)

  • प्रोजेक्ट का नामस्थान "माइक्रोसेक्चर.इमेल सेवा" कहा जाता है।

  • आदेश संसाधन फ़ाइल का नाम पाने के लिए, मैं

    GetType()। Assembly.GetManifestResourceNames()

इस्तेमाल किया और मैं निम्नलिखित

GetType मिल() .Assembly.GetManifestResourceNames() {string [2]} स्ट्रिंग [] [0] "MicroSecurity.EmailService.Services.EmailService.resources" स्ट्रिंग [1] "MicroSecurity.EmailService.Resources.resources" स्ट्रिंग

इससे मैंने पहचान की कि "MicroSecurity.EmailService.Resources.resources" वह स्ट्रिंग है जिसका मैं उपयोग करना चाहता हूं (अनुक्रमणिका 1)।

  • मैंने स्ट्रीम कोड प्राप्त करने के लिए इस कोड का उपयोग किया था।

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

जब मैं डीबगिंग के दौरान इस चर के लिए एक घड़ी जोड़ने के लिए, मैं इस तरह के आदि अपनी छवियों के लिए मेटाडाटा और

यहाँ के रूप में चीजों को देख सकते है, जहां मैं अटक कर रहा हूँ। मैं "logo.jpg" नामक छवि तक पहुंच बनाना चाहता हूं। छवि पर जाने के लिए मैं यही कर रहा हूं, लेकिन यह काम नहीं कर रहा है।

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

मैं अपने लोगो.जेपीजी फ़ाइल से स्ट्रीम कैसे प्राप्त कर सकता हूं?

अद्यतन:

एंड्रयू के लिए धन्यवाद, मैं यह पता लगाने में सक्षम था। नीचे कुछ कोड है जो मैंने डेमो प्रोजेक्ट के लिए लिखा था ताकि यह अध्ययन किया जा सके कि कैसे संसाधन फ़ाइल बनाम बनाम फ़ाइलों को सीधे एम्बेड करता है।मुझे आशा है कि इससे दूसरों को मतभेदों को स्पष्ट करने में मदद मिलेगी।

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; 
      } 
     } 
    } 
} 
+0

में परिवर्तित करने का एक उदाहरण पा सकते हैं, आप संसाधनों का नाम देखने के लिए परावर्तक के साथ असेंबली खोल सकते हैं –

उत्तर

11

आप एंबेडेड संसाधन के लिए संसाधन फ़ोल्डर में फ़ाइलें सेट करते हैं तो आप को देखा है चाहिए यह GetManifestResourceNames() कॉल में सूचीबद्ध। आप

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

नाम "MicroSecurity.EmailService.Resources.logo.jpg" होना चाहिए अगर यह संसाधन फ़ोल्डर में है की कोशिश कर सकते। हालांकि, फ़ाइल को एंबेडेड रिसोर्स के रूप में चिह्नित करना संसाधन फ़ाइल के उद्देश्य को हरा देता है (छवि स्वयं दो बार एम्बेड की जाएगी)।

आप संसाधन फ़ाइल को पूरी तरह से हटा सकते हैं और प्रत्येक फ़ाइल को एंबेडेड संसाधन के रूप में सेट कर सकते हैं। उस बिंदु पर, प्रत्येक फ़ाइल के लिए अलग-अलग प्रकट संसाधन होना चाहिए। एक सी # प्रोजेक्ट में, प्रत्येक फ़ाइल नाम प्रोजेक्ट नेमस्पेस + उप फ़ोल्डर द्वारा उपसर्ग किया जाएगा। उदाहरण के लिए। यदि आप किसी संसाधन/एम्बेडेड फ़ोल्डर में "logo.jpg" फ़ाइल जोड़ते हैं, तो संसाधन का नाम "MicroSecurity.EmailService.Resources.Embedded.logo.jpg" होगा।

वैकल्पिक रूप से, संसाधन फ़ाइल से बिटमैप प्राप्त करें और उसे स्ट्रीम में परिवर्तित करें। आप को MemoryStream में How do I convert a Bitmap to byte[]?

1

आप उपयोग कर सकते हैं:

System.Drawing.Bitmap myLogo = MicroSecurity.Properties.Resources.logo; 
संबंधित मुद्दे