如何将项目从引用复制到磁盘? (C#)

时间:2009-06-26 08:55:16

标签: c# copy reference

例如,我的参考资料中有一部flash动画。如何将其复制到应用程序之外的位置?

1 个答案:

答案 0 :(得分:3)

假设您将其作为嵌入式资源,您可以执行以下操作:

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);
            }
        }
    }
}

用以下方式调用:

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

(其中Foo.Bar.FlashFile.swf是资源的路径。)