473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use binary files as resources in VB 2005

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

Oct 15 '06 #1
4 6240
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" <ve********@gmail.comschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
>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

Oct 15 '06 #2
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().GetManifestResourc eStream(
"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.GetManife stResourceStream(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

On Oct 15, 5:37 pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
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" <version...@gmail.comschreef in berichtnews:11**********************@m7g2000cwm.go oglegroups.com...
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
Oct 15 '06 #3
Anybody pls. :(

FreewareGuy wrote:
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().GetManifestResourc eStream(
"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.GetManife stResourceStream(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

On Oct 15, 5:37 pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
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" <version...@gmail.comschreef in berichtnews:11**********************@m7g2000cwm.go oglegroups.com...
>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
Oct 16 '06 #4
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.Exten sion"))
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/Downloa...sourceTest.zip

regards

Michel Posseth [MCP]



"FreewareGuy" wrote:
Anybody pls. :(

FreewareGuy wrote:
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().GetManifestResourc eStream(
"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.GetManife stResourceStream(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

On Oct 15, 5:37 pm, "Michel Posseth [MCP]" <M...@posseth.comwrote:
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" <version...@gmail.comschreef in berichtnews:11**********************@m7g2000cwm.go oglegroups.com...
>
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

Oct 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jacob H | last post by:
Hello all, Today I began writing a utility script that takes given binary files and puts them all into one datafile. My idea is to be able to access any binary data I want by indexing the...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
11
by: rbt | last post by:
Is there an easy way to exclude binary files (I'm working on Windows XP) from the file list returned by os.walk()? Also, when reading files and you're unsure as to whether or not they are ascii...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
5
by: Franck | last post by:
Hello, I've just moved to visual developper 2005 to do so, I also had to use the convert assistant. what it did; moving my file resx files that i had in a diresctory called resx to a new...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
17
by: osama178 | last post by:
Hi, What does it mean for an object to be binary compatible? And why aren't STL objects binary compatible? Any insights, links, resources for further reading are greatly appreciated. Thanks.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.