开发者

How to copy file From Resources?

开发者 https://www.devze.com 2023-04-01 03:26 出处:网络
I have an embedded resources file 开发者_如何学运维eg: file.exe how to copy in directory eg: c:\\?

I have an embedded resources file 开发者_如何学运维eg: file.exe how to copy in directory eg: c:\? at click button thanks


You can use Assembly.GetManifestResourceStream to get a stream to read your resource from. Then just copy it to a FileStream. If you're using .NET 4, you could use Stream.CopyTo to make that easy:

private void CopyResource(string resourceName, string file)
{
    using (Stream resource = GetType().Assembly
                                      .GetManifestResourceStream(resourceName))
    {
        if (resource == null)
        {
            throw new ArgumentException("No such resource", "resourceName");
        }
        using (Stream output = File.OpenWrite(file))
        {
            resource.CopyTo(output);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消