Skip to content

Instantly share code, notes, and snippets.

@xtrmstep
Last active August 29, 2015 14:27
Show Gist options
  • Save xtrmstep/f9043bfe858fa466dbc0 to your computer and use it in GitHub Desktop.
Save xtrmstep/f9043bfe858fa466dbc0 to your computer and use it in GitHub Desktop.
Extract text files embedded as resources to an assembly to files on disk
public void ExtractEmbeddedResourcesToFiles()
{
Assembly a = typeof (SomeTypeFromAssemblyWithResources).Assembly;
string d = Path.GetDirectoryName(a.Location);
string outputDir = Path.Combine(d, subFolder);
if (Directory.Exists(outputDir) == false)
{
Directory.CreateDirectory(outputDir);
}
List<string> rs = a.GetManifestResourceNames().Where(n => n.Contains(prefix)).ToList();
foreach (string r in rs)
{
string fileName = r.Replace(prefix + ".", string.Empty);
using (Stream stream = a.GetManifestResourceStream(r))
{
if (stream != null)
{
string path = Path.Combine(outputDir, fileName);
using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(file);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment