Project: EmbeddedResources



How to embed a file resource using Visual Studio 2010:
How to embed a file resource using Visual Studio 2010

How to extract an embedded resource from a Console application, copy it to the TEMP folder an launche the .exe file (in this case):
public static void Main()
{
    var assembly = Assembly.GetExecutingAssembly();
            
    var resourceName = 
        assembly.GetName().Name + ".EyeOpen.EmbeddedExecutable.Windows.exe";

    var filePath = Path.Combine(Path.GetTempPath(), resourceName);

    ExtractResourceToFile(assembly, resourceName, filePath);

    Process.Start(filePath);
}

private static void ExtractResourceToFile(Assembly assembly, string resourceName, string filePath)
{
    using (var stream = assembly.GetManifestResourceStream(resourceName))
    {
        if (stream == null)
        {
            throw new MissingManifestResourceException();
        }

        WriteStreamToFile(stream, filePath);
    }
}

private static void WriteStreamToFile(Stream stream, string filePath)
{
    var buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);

    using (var writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
    {
        writer.Write(buffer);
    }
}

Last edited Mar 30, 2012 at 11:28 PM by teoarch, version 2

Comments

No comments yet.