How to use binary files as resources in VB 2005

F

FreewareGuy

I want to add binary files to my VB 2005 executable to make a single
exe with all the required files which I can extract at will through my
code, how do I do this pls. guide o how to add binary file (.exe) and
extract them using resource files.

I got answer for this at Vbcity forums but the code provided was in C#
, pls. help me convert it to VB 2005

http://www.vbcity.com/forums/attachment.asp?id=17127
 
M

Michel Posseth [MCP]

i do not see the problem ,,,, there is a resource editor in the IDE in fact
a resource editor was already availlable in the ide since VB6 , i cannot
see the code on devcity as i do not have an account there .

regards

Michel
 
F

FreewareGuy

Thanks for the reply, I had already added the Exe file as a resource to
my Vb 2005 project but I need to extract that to a file, I am using the
code below:

C# (WORKING)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace ExtractEmbeddedApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnExtract_Click(object sender, EventArgs e)
{
SaveFileDialog objSFD = new SaveFileDialog();
objSFD.InitialDirectory = "C:\\";
objSFD.Filter = "Executable (*.exe)|*.exe";
objSFD.Title = "Save embedded resource to...";

if (objSFD.ShowDialog() == DialogResult.OK)
{
Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"ExtractEmbeddedApp.EmbeddedApp.exe");

byte[] fileBytes = new byte[str.Length];

str.Read(fileBytes, 0, fileBytes.Length);

str.Close();
str.Dispose();

FileStream fs = File.Create(objSFD.FileName);
fs.Write(fileBytes, 0, fileBytes.Length);
fs.Flush();
fs.Close();
fs.Dispose();

Process.Start(objSFD.FileName);
}
}
}
}

I want the same functionality in VB 2005

Tried below code without sucess , produces error (Error location marked
as <<== in code below):
System.NullReferenceException was unhandled Message="Object reference
not set to an instance of an object.":

Imports System
Imports System.Windows.Forms
Imports System.Reflection
Imports System.IO

Public Class frmMain

Private Sub btnSandboxie_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnSandboxie.Click

Dim m As IO.MemoryStream = LoadResource(Me.GetType.Namespace &
".MyApp1.exe")

SaveFile("c:\app1.exe", m)
End Sub

Private Function LoadResource(ByVal ResourceName As String) As
IO.MemoryStream
Dim ResourceStream As IO.Stream
ResourceStream =
Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
If ResourceStream Is Nothing Then Return Nothing
Dim byts(CInt(ResourceStream.Length - 1)) As Byte
Dim Len As Integer = ResourceStream.Read(byts, 0,
CInt(ResourceStream.Length))
Dim MemStream As New IO.MemoryStream(byts, 0, Len)
Return MemStream
End Function

Private Sub SaveFile(ByVal FilePath As String, ByVal mstream As
System.IO.MemoryStream)
'save file to path specified
Dim FS As New FileStream(FilePath, IO.FileMode.Create,
IO.FileAccess.Write)
mstream.WriteTo(FS)
mstream.Flush()
FS.Close()
End Sub
End Class
 
F

FreewareGuy

Anybody pls. :(
Thanks for the reply, I had already added the Exe file as a resource to
my Vb 2005 project but I need to extract that to a file, I am using the
code below:

C# (WORKING)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace ExtractEmbeddedApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnExtract_Click(object sender, EventArgs e)
{
SaveFileDialog objSFD = new SaveFileDialog();
objSFD.InitialDirectory = "C:\\";
objSFD.Filter = "Executable (*.exe)|*.exe";
objSFD.Title = "Save embedded resource to...";

if (objSFD.ShowDialog() == DialogResult.OK)
{
Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"ExtractEmbeddedApp.EmbeddedApp.exe");

byte[] fileBytes = new byte[str.Length];

str.Read(fileBytes, 0, fileBytes.Length);

str.Close();
str.Dispose();

FileStream fs = File.Create(objSFD.FileName);
fs.Write(fileBytes, 0, fileBytes.Length);
fs.Flush();
fs.Close();
fs.Dispose();

Process.Start(objSFD.FileName);
}
}
}
}

I want the same functionality in VB 2005

Tried below code without sucess , produces error (Error location marked
as <<== in code below):
System.NullReferenceException was unhandled Message="Object reference
not set to an instance of an object.":

Imports System
Imports System.Windows.Forms
Imports System.Reflection
Imports System.IO

Public Class frmMain

Private Sub btnSandboxie_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnSandboxie.Click

Dim m As IO.MemoryStream = LoadResource(Me.GetType.Namespace &
".MyApp1.exe")

SaveFile("c:\app1.exe", m)
End Sub

Private Function LoadResource(ByVal ResourceName As String) As
IO.MemoryStream
Dim ResourceStream As IO.Stream
ResourceStream =
Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
If ResourceStream Is Nothing Then Return Nothing
Dim byts(CInt(ResourceStream.Length - 1)) As Byte
Dim Len As Integer = ResourceStream.Read(byts, 0,
CInt(ResourceStream.Length))
Dim MemStream As New IO.MemoryStream(byts, 0, Len)
Return MemStream
End Function

Private Sub SaveFile(ByVal FilePath As String, ByVal mstream As
System.IO.MemoryStream)
'save file to path specified
Dim FS As New FileStream(FilePath, IO.FileMode.Create,
IO.FileAccess.Write)
mstream.WriteTo(FS)
mstream.Flush()
FS.Close()
End Sub
End Class

i do not see the problem ,,,, there is a resource editor in the IDE in fact
a resource editor was already availlable in the ide since VB6 , i cannot
see the code on devcity as i do not have an account there .

regards

Michel
 
G

Guest

sorry ... i lost track of this thread in my newsgroup reader

The answer to your problem is simple , bit odd that those guys at vbcity
showed you a C# example it isn`t C# city isn`t it ?? :)


<imports>
Option Strict On
Option Explicit On
Imports System
Imports System.Reflection
Imports System.IO
</imports>

<methods>
Private Function GetResourceStream(ByVal resfile As String) As Stream
Dim asm As Assembly = Assembly.GetExecutingAssembly
Return asm.GetManifestResourceStream(resfile)
End Function
</methods>

<Usage>

With (GetResourceStream("NameSpaceOfProg.Filename.Extension"))
Dim count As Integer
Dim byteArray As Byte()

' Read the first 20 bytes from the stream.
byteArray = _
New Byte(CType(.Length, Integer)) {}
count = .Read(byteArray, 0, 20)

' Read the remaining Bytes, Byte by Byte.
While (count < .Length)
byteArray(count) = _
Convert.ToByte(.ReadByte())
count += 1
End While

Using sw As Stream = File.Open("Filename.Extension",
FileMode.Create, FileAccess.Write)
'create a binary write so we can use this to access the
underlying stream
Using bw As New BinaryWriter(sw)
'write the byte array in one action
bw.Write(byteArray)
bw.Close()
End Using
sw.Close()
End Using
End With



</usage>


you can download a working and tested example here , this project extracts a
Access mdb to the working directory from the resource manifest

http://www.vbdotnetcoder.com/Downloads/Examples/authormp/ExtractFromResourceTest.zip

regards

Michel Posseth [MCP]











FreewareGuy said:
Anybody pls. :(
Thanks for the reply, I had already added the Exe file as a resource to
my Vb 2005 project but I need to extract that to a file, I am using the
code below:

C# (WORKING)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace ExtractEmbeddedApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnExtract_Click(object sender, EventArgs e)
{
SaveFileDialog objSFD = new SaveFileDialog();
objSFD.InitialDirectory = "C:\\";
objSFD.Filter = "Executable (*.exe)|*.exe";
objSFD.Title = "Save embedded resource to...";

if (objSFD.ShowDialog() == DialogResult.OK)
{
Stream str =
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"ExtractEmbeddedApp.EmbeddedApp.exe");

byte[] fileBytes = new byte[str.Length];

str.Read(fileBytes, 0, fileBytes.Length);

str.Close();
str.Dispose();

FileStream fs = File.Create(objSFD.FileName);
fs.Write(fileBytes, 0, fileBytes.Length);
fs.Flush();
fs.Close();
fs.Dispose();

Process.Start(objSFD.FileName);
}
}
}
}

I want the same functionality in VB 2005

Tried below code without sucess , produces error (Error location marked
as <<== in code below):
System.NullReferenceException was unhandled Message="Object reference
not set to an instance of an object.":

Imports System
Imports System.Windows.Forms
Imports System.Reflection
Imports System.IO

Public Class frmMain

Private Sub btnSandboxie_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnSandboxie.Click

Dim m As IO.MemoryStream = LoadResource(Me.GetType.Namespace &
".MyApp1.exe")

SaveFile("c:\app1.exe", m)
End Sub

Private Function LoadResource(ByVal ResourceName As String) As
IO.MemoryStream
Dim ResourceStream As IO.Stream
ResourceStream =
Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
If ResourceStream Is Nothing Then Return Nothing
Dim byts(CInt(ResourceStream.Length - 1)) As Byte
Dim Len As Integer = ResourceStream.Read(byts, 0,
CInt(ResourceStream.Length))
Dim MemStream As New IO.MemoryStream(byts, 0, Len)
Return MemStream
End Function

Private Sub SaveFile(ByVal FilePath As String, ByVal mstream As
System.IO.MemoryStream)
'save file to path specified
Dim FS As New FileStream(FilePath, IO.FileMode.Create,
IO.FileAccess.Write)
mstream.WriteTo(FS)
mstream.Flush()
FS.Close()
End Sub
End Class

i do not see the problem ,,,, there is a resource editor in the IDE in fact
a resource editor was already availlable in the ide since VB6 , i cannot
see the code on devcity as i do not have an account there .

regards

Michel

"FreewareGuy" <[email protected]> schreef in bericht
I want to add binary files to my VB 2005 executable to make a single
exe with all the required files which I can extract at will through my
code, how do I do this pls. guide o how to add binary file (.exe) and
extract them using resource files.

I got answer for this at Vbcity forums but the code provided was in C#
, pls. help me convert it to VB 2005

http://www.vbcity.com/forums/attachment.asp?id=17127
 

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