2012-10-23 16 views
32

मैं CSharpCodeProvider का उपयोग कर नीचे दिए गए कोड को संकलित करने का प्रयास कर रहा हूं। फ़ाइल सफलतापूर्वक संकलित की गई है, लेकिन जब मैं जेनरेट की गई EXE फ़ाइल पर क्लिक करता हूं, तो मुझे एक त्रुटि मिलती है (विंडोज इस समस्या के समाधान की खोज कर रहा है) और कुछ भी नहीं होता है।मैं एम्बेडेड संसाधन से फ़ाइल कैसे निकाल सकता हूं और इसे डिस्क पर सहेज सकता हूं?

जब मैं CSharpCodeProvider का उपयोग कर नीचे कोड संकलन, मैं कोड की इस पंक्ति का उपयोग कर एक एम्बेडेड संसाधन फ़ाइल के रूप में MySql.Data.dll जोड़ दिया है:

if (provider.Supports(GeneratorSupport.Resources)) 
    cp.EmbeddedResources.Add("MySql.Data.dll"); 

फ़ाइल सफलतापूर्वक एम्बेडेड है (क्योंकि मैं फ़ाइल आकार में वृद्धि हुई है देखा)।

नीचे दिए गए कोड में, मैं एम्बेडेड DLL फ़ाइल निकालने और इसे System32 पर सहेजने का प्रयास करता हूं, लेकिन नीचे दिया गया कोड किसी कारण से काम नहीं करता है।

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public static void ExtractSaveResource(String filename, String location) 
     { 
      //Assembly assembly = Assembly.GetExecutingAssembly(); 
      Assembly a = .Assembly.GetExecutingAssembly(); 
      //Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever 
      //string my_namespace = a.GetName().Name.ToString(); 
      Stream resFilestream = a.GetManifestResourceStream(filename); 
      if (resFilestream != null) 
      { 
       BinaryReader br = new BinaryReader(resFilestream); 
       FileStream fs = new FileStream(location, FileMode.Create); // Say 
       BinaryWriter bw = new BinaryWriter(fs); 
       byte[] ba = new byte[resFilestream.Length]; 
       resFilestream.Read(ba, 0, ba.Length); 
       bw.Write(ba); 
       br.Close(); 
       bw.Close(); 
       resFilestream.Close(); 
      } 
      // this.Close(); 
     } 

     static void Main(string[] args) 
     { 
      try 
      { 
       string systemDir = Environment.SystemDirectory; 
       ExtractSaveResource("MySql.Data.dll", systemDir); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       Console.ReadKey(); 
      } 
     } 
    } 
} 

मैं संसाधन के रूप में एम्बेडेड DLL फ़ाइल को कैसे निकाल सकता हूं और इसे System32 पर सहेज सकता हूं?

+0

रफ़ीक, आप नीचे थॉमस सैप के उत्तरों की समीक्षा करनी चाहिए क्योंकि यह आपके द्वारा स्वीकार किए जाने वाले किसी भी व्यक्ति से बिल्कुल बेहतर है। – motoDrizzt

उत्तर

31

मैं ने पाया है कि Properties.Resources उपयोग करने के लिए है यह करने के लिए सबसे आसान तरीका और FileFile.WriteAllBytes(fileName, Properties.Resources.file);

पाठ फ़ाइलों के लिए:: यहाँ कोड मैं का उपयोग करें ...

बाइनरी फ़ाइलों के लिए है File.WriteAllText(fileName, Properties.Resources.file);

+6

इतना आसान है। यह स्वीकार्य उत्तर होना चाहिए। –

+1

और यह विजुअल स्टूडियो 2005/.NET 2.0 के बाद से काम करता है! मुझे नहीं पता कि 2012 के उत्तरों को क्या उचित ठहराता है: पी –

+0

आपको पहले संसाधन फ़ाइल बनाने की आवश्यकता है ... वहां फ़ाइलों को डालने के माध्यम से चलने के लिए उपयोगी हो सकता है, आदि –

0

एक MemoryStream में अपने लक्ष्य विधानसभा पढ़ने और फिर (ध्यान में रखना कृपया है कि इस कोड का परीक्षण नहीं किया गया है) एक FileStream इस तरह के बचत का प्रयास करें:

Assembly assembly = Assembly.GetExecutingAssembly(); 

using (var target = assembly.GetManifestResourceStream("MySql.Data.dll")) 
{ 
    var size = target.CanSeek ? Convert.ToInt32(target.Length) : 0; 

    // read your target assembly into the MemoryStream 
    MemoryStream output = null; 
    using (output = new MemoryStream(size)) 
    { 
     int len; 
     byte[] buffer = new byte[2048]; 

     do 
     { 
      len = target.Read(buffer, 0, buffer.Length); 
      output.Write(buffer, 0, len); 
     } 
     while (len != 0); 
    } 

    // now save your MemoryStream to a flat file 
    using (var fs = File.OpenWrite(@"c:\Windows\System32\MySql.Data.dll")) 
    { 
     output.WriteTo(fs); 
     fs.Flush(); 
     fs.Close() 
    } 
} 
31

मैं इस का उपयोग कर (परीक्षण किया) विधि:

OutputDir: स्थान जहां संसाधन कॉपी करने के लिए

ResourceLocation हैं: नाम स्थान (+ dirnames)

फ़ाइलें: रिसोरसेलोकेशन के भीतर फ़ाइलों की सूची, आप कॉपी करना चाहते हैं।

private static void ExtractEmbeddedResource(string outputDir, string resourceLocation, List<string> files) 
    { 
     foreach (string file in files) 
     { 
      using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceLocation + @"." + file)) 
      { 
       using (System.IO.FileStream fileStream = new System.IO.FileStream(System.IO.Path.Combine(outputDir, file), System.IO.FileMode.Create)) 
       { 
        for (int i = 0; i < stream.Length; i++) 
        { 
         fileStream.WriteByte((byte)stream.ReadByte()); 
        } 
        fileStream.Close(); 
       } 
      } 
     } 
    } 
50

मैं सुझाव देना चाहता हूं कि यह आसान हो। मुझे लगता है कि संसाधन मौजूद है और फ़ाइल लिखने योग्य है (यदि हम सिस्टम निर्देशिकाओं के बारे में बात कर रहे हैं तो यह एक मुद्दा हो सकता है)।

public void WriteResourceToFile(string resourceName, string fileName) 
{ 
    using(var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) 
    { 
     using(var file = new FileStream(fileName, FileMode.Create, FileAccess.Write)) 
     { 
      resource.CopyTo(file); 
     } 
    } 
} 
+2

इसके उल्लेख का उल्लेख करने के लिए .NET 4.0 या ऊपर –

1

यह पूरी तरह से काम करता है! उपयोग के

public static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName) 
{ 
    //nameSpace = the namespace of your project, located right above your class' name; 
    //outDirectory = where the file will be extracted to; 
    //internalFilePath = the name of the folder inside visual studio which the files are in; 
    //resourceName = the name of the file; 
    Assembly assembly = Assembly.GetCallingAssembly(); 

    using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName)) 
    using (BinaryReader r = new BinaryReader(s)) 
    using (FileStream fs = new FileStream(outDirectory + "\\" + resourcename, FileMode.OpenOrCreate)) 
    using (BinaryWriter w = new BinaryWriter(fs)) 
    { 
     w.Write(r.ReadBytes((int)s.Length)); 
    } 
} 

उदाहरण:

public static void ExtractFile() 
{ 
    String local = Environment.CurrentDirectory; //gets current path to extract the files 

    Extract("Geral", local, "Arquivos", "bloquear_vbs.vbs"); 
}  

यदि इससे भी मदद नहीं करता है, यह वीडियो का प्रयास करें: https://www.youtube.com/watch?v=_61pLVH2qPk

0

या एक विस्तार विधि का उपयोग कर ...

/// <summary> 
/// Retrieves the specified [embedded] resource file and saves it to disk. 
/// If only filename is provided then the file is saved to the default 
/// directory, otherwise the full filepath will be used. 
/// <para> 
/// Note: if the embedded resource resides in a different assembly use that 
/// assembly instance with this extension method. 
/// </para> 
/// </summary> 
/// <example> 
/// <code> 
///  Assembly.GetExecutingAssembly().ExtractResource("Ng-setup.cmd"); 
///  OR 
///  Assembly.GetExecutingAssembly().ExtractResource("Ng-setup.cmd", "C:\temp\MySetup.cmd"); 
/// </code> 
/// </example> 
/// <param name="assembly">The assembly.</param> 
/// <param name="resourceName">Name of the resource.</param> 
/// <param name="fileName">Name of the file.</param> 
public static void ExtractResource(this Assembly assembly, string filename, string path=null) 
{ 
    //Construct the full path name for the output file 
    var outputFile = path ?? [email protected]"{Directory.GetCurrentDirectory()}\{filename}"; 

    // If the project name contains dashes replace with underscores since 
    // namespaces do not permit dashes (underscores will be default to). 
    var resourceName = $"{assembly.GetName().Name.Replace("-","_")}.{filename}"; 

    // Pull the fully qualified resource name from the provided assembly 
    using (var resource = assembly.GetManifestResourceStream(resourceName)) 
    { 
     if (resource == null) 
      throw new FileNotFoundException($"Could not find [{resourceName}] in {assembly.FullName}!"); 

     using (var file = new FileStream(outputFile, FileMode.Create, FileAccess.Write)) 
     { 
      resource.CopyTo(file); 
     } 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे