How to extract a resource to a file

B

Bob Altman

Hi all,

I seem to recall seeing a method that extracts a resource from the executing
assembly's manifest and writes it to a file, freeing me from having to write
the code that gets a stream via Assembly.GetManifestResourceStream, reads
the stream, and writes the file. I thought it was under the My namespace,
but I can't find it. Maybe it came to me in a (wishful) dream...

TIA - Bob
 
T

Tom Shelton

Hi all,

I seem to recall seeing a method that extracts a resource from the executing
assembly's manifest and writes it to a file, freeing me from having to write
the code that gets a stream via Assembly.GetManifestResourceStream, reads
the stream, and writes the file. I thought it was under the My namespace,
but I can't find it. Maybe it came to me in a (wishful) dream...

TIA - Bob

Bob... not sure about anything under the My namespace. I usally avoid
anything in My - I'll use the real framework, thank you very much :)

But, the code to write the resource to a file is pretty short none the less...

Using (s As Stream = _
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.ATextFile.txt"))
Dim bytes(s.Length) As Byte
s.Read(bytes, 0, bytes.Length)
File.WriteAllBytes("outpath.txt", bytes)
End Using
 
H

Hongye Sun [MSFT]

Thanks for Tom's answer.

Hi Bob,

As far as I know, there is no such a method to extract a resource from the
executing assembly's manifest and writes it to a file. However, as Tom
said, we can implement it by ourselves. I implemented the following code as
a sample for you:
----------------------------------------------------------
Public Shared Sub ExtractResourceFile(ByVal resouceName As String)
Using s As Stream =
Assembly.GetExecutingAssembly.GetManifestResourceStream(resouceName)
Using b As BufferedStream = New BufferedStream(s)
If File.Exists(resouceName) Then
File.Delete(resouceName)
End If
Using f As FileStream = New FileStream(resouceName,
FileMode.OpenOrCreate)
Dim _res As Integer
Do While (_res = b.ReadByte >= 0)
f.WriteByte(CByte(_res))
Loop
End Using
End Using
End Using
End Sub

Public Shared Sub ExtractResouceFiles()
Dim resNames As String() =
Assembly.GetExecutingAssembly.GetManifestResourceNames
Dim resName As String
For Each resName In resNames
ExtractResourceFile(resName)
Next
End Sub
----------------------------------------------------------

Please let me know if they works for you. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within?2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bob Altman

Thanks Tom and Hongye. I, too, have written several variations on that same
code. I just thought that I remembered seeing the whole "extract a resource
to a file" thing baked into the FCL somewhere. Oh well...

Bob
 
H

Hongye Sun [MSFT]

Hi Bob,

I use reflector to analyze the GetManifestResourceStream method to check
who uses it in FCL. Here is what I found:

System.Reflection.Assembly.GetManifestResourceStream(Type, String) : Stream
Depends On
Used By
System.Drawing.Bitmap..ctor(Type, String)
System.Drawing.Icon..ctor(Type, String)
System.Drawing.ToolboxBitmapAttribute..cctor()
System.Drawing.ToolboxBitmapAttribute.GetBitmapFromResource(Type, String,
Boolean) : Image
System.Drawing.ToolboxBitmapAttribute.GetIconFromResource(Type, String,
Boolean) : Image
System.Windows.Forms.Cursor..ctor(String, Int32)
System.Windows.Forms.Cursor..ctor(Type, String)

It shows that some types have shortcut constructor to get the resource from
type's assembly. However, there is no such method to extract a resource
to a file directly.

Hope it helps. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bob Altman

"Hongye Sun [MSFT]" said:
Hi Bob,

I use reflector to analyze the GetManifestResourceStream method to check
who uses it in FCL. Here is what I found:


That was creative! Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top