Saving files from a Resource  
Author Message
Magos294963





PostPosted: .NET Base Class Library, Saving files from a Resource Top

I've added a file to my project's resource file (a database file, Empty.mdb, to be used as a template later on). Now, when running the program, how can I save this file (from the reosurce) to a real physical file

I've looked into the System.Resources classes but they seem to be handling the actual *.resx files etc, not resources during runtime.



.NET Development4  
 
 
John Musenee





PostPosted: .NET Base Class Library, Saving files from a Resource Top

This ought to do the trick:

stream = GetResourceStream(System.Reflection.Assembly.GetCallingAssembly(), aFileName)

If stream Is Nothing Then

stream = GetResourceStream(System.Reflection.Assembly.GetExecutingAssembly(), aFileName)

If stream Is Nothing Then

stream = GetResourceStream(System.Reflection.Assembly.GetEntryAssembly(), aFileName)

End If

End If

Probably you only need the GetExecutingAssembly.

Kind regards,

Jhn


 
 
Magos





PostPosted: .NET Base Class Library, Saving files from a Resource Top

What is GetResourceStream Couldn't find any reference to it. At any rate, your code gave me some hints so now I have a working saver using Assembly, ResourceManager and File.
 
 
John Musenee





PostPosted: .NET Base Class Library, Saving files from a Resource Top

Oopst, i chose that name so good that i forgot it isn't a .NET function:

Private Function GetResourceStream(ByVal assemb As System.Reflection.Assembly, ByVal asResourceName As String) As IO.Stream

Dim stream As IO.Stream = Nothing

Dim fullName As String = LookForResource(assemb, asResourceName)

If Not fullName Is Nothing Then

stream = assemb.GetManifestResourceStream(fullName)

End If

Return stream

End Function

Private Shared Function LookForResource(ByVal assem As System.Reflection.Assembly, ByVal asResourceName As String) As String

Dim names() As String = assem.GetManifestResourceNames()

For Each name As String In names

If name.Equals(asResourceName) Then

Return name

ElseIf name.EndsWith("." & asResourceName) Then

Return name

End If

Next name

Return Nothing

End Function