Last active
August 29, 2015 14:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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