Mats
Mats

Reputation:

How can I copy an item from references to disk? (c#)

For example, I have a flash movie in my references. How do I copy this to a location outside of the application?

Upvotes: 0

Views: 188

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499800

Assuming you have it as an embedded resource, you'd do something like:

public static void WriteResourceToDisk(Assembly assembly, 
                                       string resource,
                                       string file)
{
    using (Stream input = assembly.GetManifestResourceStream(resource))
    {
        if (input == null)
        {
            throw new ArgumentException("Resource name not found");
        }
        using (Stream output = File.Create(file))
        {
            byte[] buffer = new byte[8 * 1024];
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }
        }
    }
}

Call it with:

WriteResourceToDisk(typeof(SomeKnownType).Assembly, 
                    "Foo.Bar.FlashFile.swf", "file.swf");

(Where Foo.Bar.FlashFile.swf is the path to the resource.)

Upvotes: 3

Related Questions