Copying an embedded resource as a file to disk in C #

I have an INF file saved as an embedded resource in my C # project. I am trying to save this file in a local place on demand. I am using this method.

public static void SaveResourceToDisk(string ResourceName, string FileToExtractTo) { Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName); FileStream resourceFile = new FileStream(FileToExtractTo, FileMode.Create); byte[] b = new byte[s.Length + 1]; s.Read(b, 0, Convert.ToInt32(s.Length)); resourceFile.Write(b, 0, Convert.ToInt32(b.Length - 1)); resourceFile.Flush(); resourceFile.Close(); resourceFile = null; } 

When I try to call this method (passing the resource name along with the namespace name), I get an error message:

Object reference not set to object instance

What am I doing wrong here?

+9
c # embedded-resource
source share
4 answers

You may call

 System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); 

And check which built-in resources are available. You can then compare this to what you are going through to see if you are really doing what you expect.

In addition, you should consider using using the keyword to get rid of your threads:

 using(FileStream ResourceFile = new FileStream(FileToExtractTo, FileMode.Create)) { //do stuff } 

Good luck.

+9
source share

This is the easiest way to save the embedded resource:

  var stream = assembly.GetManifestResourceStream("name of the manifest resourse"); var fileStream = File.Create(@"C:\Test.xml"); stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); fileStream.Close(); 
+3
source share

Unpacks all embedded resources inside the assembly and saves them together with the assembly containing this class, while maintaining the directory structure. This code does not require you to know anything about the files contained in the assembly, and is a more general solution, but assumes that all files have file extensions.

 public class EmbeddedResources { private bool isUnpacked = false; public async Task EnsureUnpacked(string saveDirectory) { if (!this.isUnpacked) { var assembly = Assembly.GetExecutingAssembly(); var assemblyDirectory = Path.GetDirectoryName(assembly.Location); foreach (var name in assembly.GetManifestResourceNames()) { var stream = assembly.GetManifestResourceStream(name); var stringBuilder = new StringBuilder(); var parts = name .Replace(typeof(EmbeddedResources).Namespace + ".", string.Empty) .Split('.') .ToList(); for (int i = 0; i < parts.Count; ++i) { var part = parts[i]; if (string.Equals(part, string.Empty)) { stringBuilder.Append("."); // Append '.' in file name. } else if (i == parts.Count - 2) { stringBuilder.Append(part); // Append file name and '.'. stringBuilder.Append('.'); } else if (i == parts.Count - 1) { stringBuilder.Append(part); // Append file extension. } else { stringBuilder.Append(part); // Append file path. stringBuilder.Append('\\'); } } var filePath = Path.Combine(saveDirectory, stringBuilder.ToString()); using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { await stream.CopyToAsync(fileStream); } } this.isUnpacked = true; } } } 
+1
source share

attach your file to your project. click the file.go file attachment in the properties, set the "Build Action" parameter to "Embedded Resource", use the file name in the code, for example: "YourProjectName.YourFileNameIWithExt"

0
source share

All Articles