How I can read textfile contained in the exe

S

Samir Ibrahim

Hi All

I don't know if my question is right, but that is how I understand it.

In the vb2008 project if I add a text file let say (c:\abc.txt) to my
project,
this file will be copied to the project folder, then if I click on the
abc.txt from
my project a property window will displayed, one of the property there is
"build action", which may have one of the following
None,Compile,Content,Embedded
Resource,ApplicationDefinition,Page,Resource,SplashScreen,EntityDeploy

What should I choose in order to let this txt file included in the exe and
how I can later read it.

Thanks in advance
 
J

JR

You can add it by the projects settings. it must be a resource. than
can read it throu a streamreader.
My.Resources.yourtextfilesname

the resource name is case SENSETIVE


Take some look at resources
Jan
 
F

Family Tree Mike

Samir said:
Hi All

I don't know if my question is right, but that is how I understand it.

In the vb2008 project if I add a text file let say (c:\abc.txt) to my
project,
this file will be copied to the project folder, then if I click on the
abc.txt from
my project a property window will displayed, one of the property there is
"build action", which may have one of the following
None,Compile,Content,Embedded
Resource,ApplicationDefinition,Page,Resource,SplashScreen,EntityDeploy

What should I choose in order to let this txt file included in the exe
and how I can later read it.

Thanks in advance

Set the property to 'Embedded Resource'. You can then read it as follows:

Sub Main()
Dim s As Stream
Dim executing_assembly As System.Reflection.Assembly _
= Assembly.GetEntryAssembly()
Dim my_namespace As String _
= executing_assembly.GetName().Name.ToString()
s = executing_assembly.GetManifestResourceStream _
my_namespace + ".abc.txt")

Dim sr As New System.IO.StreamReader(s)
Dim msg As String = sr.ReadToEnd
Console.WriteLine(msg)
Console.ReadKey()
End Sub
 
S

Samir Ibrahim

Hi Jan,
That works very well. Thank you.

Hi Mike
That was just perfect, although I did not understand a thing. lol

Another question regarding this.
- I have a 1 textfile, 1 Excel, 2 Access 2007 Database which I want to
include in my exe
- I manage to read the textfile using resources and System.Reflection
- I manage to extract excel file from resources to my hdd by this link
http://social.msdn.microsoft.com/Fo...950-aef4-4a3c-b39a-d453b8320324?prof=required
- I guess I can do the same for the first access 2007 and extract it to a
folder.

But can I use the second access 2007 which I want to read rows from it using
either resources or System.Reflection without extracting it?

Thanks.
 
J

JR

Hi Jan,
That works very well. Thank you.

Hi Mike
That was just perfect, although I did not understand a thing. lol

Another question regarding this.
- I have a 1 textfile, 1 Excel, 2 Access 2007 Database which I want to
include in my exe
- I manage to read the textfile using resources and System.Reflection
- I manage to extract excel file from resources to my hdd by this linkhttp://social.msdn.microsoft.com/Forums/en/Vsexpressvb/thread/d481095...
- I guess I can do the same for the first access 2007 and extract it to a
folder.

But can I use the second access 2007 which I want to read rows from it using
either resources or System.Reflection without extracting it?

Thanks.







- Tekst uit oorspronkelijk bericht weergeven -
Hi,

I'm always a little vague with the awnser. So you also have to look a
bit :)

Conserning your Access files However you will have to create 2 access
aplications fort the 2 files, I think it will be easier to open them
with ado.

something to think about:
I don't think you will be able to lood them again into your exe file.
You already have 4 files as a resource. Maybe the exe will be very
large.

Jan
 
F

Family Tree Mike

Samir said:
Hi Jan,
That works very well. Thank you.

Hi Mike
That was just perfect, although I did not understand a thing. lol

Another question regarding this.
- I have a 1 textfile, 1 Excel, 2 Access 2007 Database which I want to
include in my exe
- I manage to read the textfile using resources and System.Reflection
- I manage to extract excel file from resources to my hdd by this link
http://social.msdn.microsoft.com/Fo...950-aef4-4a3c-b39a-d453b8320324?prof=required

- I guess I can do the same for the first access 2007 and extract it to
a folder.

But can I use the second access 2007 which I want to read rows from it
using either resources or System.Reflection without extracting it?

Thanks.

You will need to extract both of the access databases to use them.
There is no way that MS Access, nor a connection string, can connect to
the access file data while it resides inside your executable.

Sorry, I should have commented the code more... The variable
executing_assembly is a reference to the executing code, an exectuable
or dll. GetManifestResourceStream just gets a stream of data embedded
in the assembly. The My_Namespace variable is necessary as the resource
streams are not named "abc.txt", but rather "namespace.abc.text", when
built.
 
S

Samir Ibrahim

Hello Jan

JR said:
Hi,

I'm always a little vague with the awnser. So you also have to look a
bit :)

Little vague is much easier than reading Chinese As Mike response lol
Conserning your Access files However you will have to create 2 access
aplications fort the 2 files, I think it will be easier to open them
with ado.

That is the idea, one Access file should handle information belong to the
excel file and I just want to read it from the EXE using ADO , Can I ? (just
to add, not very important demand)
The other file will be extracted and the excel file should read it using
odbc,
something to think about:
I don't think you will be able to lood them again into your exe file.
You already have 4 files as a resource. Maybe the exe will be very
large.

Jan

You are absolutely correct, but this exe work as a management only, the user
will click on (new project from the menu), this should create new (extract)
Excel file, New Access File, create dsn, and link the Excel to get query
from the Access and when that finished, the user will work on the Excel all
the time.

Thank you for your advice.

Samir Ibrahim
 
S

Samir Ibrahim

Family Tree Mike said:
You will need to extract both of the access databases to use them. There
is no way that MS Access, nor a connection string, can connect to the
access file data while it resides inside your executable.

Sorry, I should have commented the code more... The variable
executing_assembly is a reference to the executing code, an exectuable or
dll. GetManifestResourceStream just gets a stream of data embedded in the
assembly. The My_Namespace variable is necessary as the resource streams
are not named "abc.txt", but rather "namespace.abc.text", when built.

Hello Mike,

About reading Access from exe, that was not an important task in my project
but more like a curiosity.

I will study more how things work in the reflection, but right now that is
enough :)

Thank you for this explanation and this cool answer.
 

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